Spring Boot快速搭建Restful Web应用

前言

  • JDK 1.8 or later
  • Maven 3.2+
  • IDE:IntelliJ IDEA

新建Maven工程

新建maven project step1

上图2,需要选择本地的jdk环境(建议1.8及以上)

新建maven project step2

构建pom文件



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.jsou
    test_demo
    1.0-SNAPSHOT
    
    test_demo
    
    Demo project for Spring Boot

    
    
        
        8
        8
        
        1.8
    

    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

整体的包结构

项目结构

编写视图层实体

public class HelloVO {
    private final long id;
    private final String content;

    public HelloVO(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

编写Controller

@RestController("/demo")
public class HelloController {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/hello/{name}")
    public HelloVO hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new HelloVO(counter.incrementAndGet(), String.format(template, name));
    }
}

编写主启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

启动主启动类

启动主启动类

启动截图

curl模拟请求

curl -X GET -G 'localhost:8080/demo/hello'
curl -X GET -G 'localhost:8080/demo/hello' -d 'name=User'

请求结果

你可能感兴趣的:(Spring Boot快速搭建Restful Web应用)