spring-boot第一章:快速开始
Posted yvanchen1992
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-boot第一章:快速开始相关的知识,希望对你有一定的参考价值。
快速开始
创建pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.github.yvanchen</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>blog</name>
<description>博客系统</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写启动类
package com.github.yvanchen.blog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author evan.chen
* @date 2020/1/9 17:20
*/
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
SpringApplication.run(BlogApplication.class, args);
}
}
编写控制器接口
package com.github.yvanchen.blog.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author evan.chen
* @date 2020/1/9 17:22
*/
@RestController
@RequestMapping
public class IndexController {
}
接口返回字符串
@GetMapping
public String index(){
return "welcome to blog!";
}
测试请求:
http://localhost:8080
响应:
welcome to blog!
接口返回自定义对象
创建Article.java
package com.github.yvanchen.blog.entity;
/**
* @author evan.chen
* @date 2020/1/9 17:39
*/
public class Article {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
编写接口
@GetMapping("articles")
public List<Article> articles() {
List<Article> articles = new ArrayList<>();
Article article = new Article();
article.setTitle("spring-boot从零打造博客系统");
articles.add(article);
return articles;
}
测试请求:
http://localhost:8080/articles
响应:
[
{
"title": "spring-boot从零打造博客系统"
}
]
至此我们已经通过简单的代码编写让我们的博客项目可以返回字符串和json对象了。
以上是关于spring-boot第一章:快速开始的主要内容,如果未能解决你的问题,请参考以下文章
第一章 Net 5.0 快速开发框架 YC.Boilerplate--框架介绍
解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段