快速开始

创建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对象了。

版权声明:本文为yvanchen1992原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yvanchen1992/p/12172742.html