码迷,mamicode.com
首页 > 编程语言 > 详细

spring-boot第一章:快速开始

时间:2020-01-09 18:55:26      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:hot   block   list   lock   odi   snapshot   xmlns   snap   ram   

快速开始

创建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第一章:快速开始

标签:hot   block   list   lock   odi   snapshot   xmlns   snap   ram   

原文地址:https://www.cnblogs.com/yvanchen1992/p/12172742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!