1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.github.yvanchen</groupId>
  12. <artifactId>blog</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>blog</name>
  15. <description>博客系统</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. <exclusions>
  29. <exclusion>
  30. <groupId>org.junit.vintage</groupId>
  31. <artifactId>junit-vintage-engine</artifactId>
  32. </exclusion>
  33. </exclusions>
  34. </dependency>
  35. </dependencies>
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>
  1. package com.github.yvanchen.blog;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @author evan.chen
  6. * @date 2020/1/9 17:20
  7. */
  8. @SpringBootApplication
  9. public class BlogApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(BlogApplication.class, args);
  12. }
  13. }
  1. package com.github.yvanchen.blog.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * @author evan.chen
  6. * @date 2020/1/9 17:22
  7. */
  8. @RestController
  9. @RequestMapping
  10. public class IndexController {
  11. }
  1. @GetMapping
  2. public String index(){
  3. return "welcome to blog!";
  4. }

测试请求:

http://localhost:8080

响应:

  1. welcome to blog!
  1. package com.github.yvanchen.blog.entity;
  2. /**
  3. * @author evan.chen
  4. * @date 2020/1/9 17:39
  5. */
  6. public class Article {
  7. private String title;
  8. public String getTitle() {
  9. return title;
  10. }
  11. public void setTitle(String title) {
  12. this.title = title;
  13. }
  14. }
  1. @GetMapping("articles")
  2. public List<Article> articles() {
  3. List<Article> articles = new ArrayList<>();
  4. Article article = new Article();
  5. article.setTitle("spring-boot从零打造博客系统");
  6. articles.add(article);
  7. return articles;
  8. }

测试请求:

http://localhost:8080/articles

响应:

  1. [
  2. {
  3. "title": "spring-boot从零打造博客系统"
  4. }
  5. ]

至此我们已经通过简单的代码编写让我们的博客项目可以返回字符串和json对象了。

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