1. 程式人生 > >spring boot開發REST接口

spring boot開發REST接口

solid comm app pen tro ren tle -s images

1.配置pom.xml文件的<parent>和<depencencies>,指定spring boot web依賴

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository 
--> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>com.wangxiaobao</groupId> <artifactId>wxb-common</artifactId> <version>1.0-SNAPSHOT</version> </
dependency> </dependencies>

2.AppMain(可以隨意起名)類

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

3.RestController 實現簡單的字符串倒序邏輯。

@RestController
public class TestService
{
    @PostMapping(value = "/test")
    public String test(String input)
    {
        return new StringBuffer(input).reverse().toString();
    }
}

4.啟動AppMain,Run as Java Application.測試成功(使用post-man):

技術分享

spring boot開發REST接口