Springboot快速构建restful接口

来源:互联网 发布:淘宝有好货报名入口 编辑:程序博客网 时间:2024/05/18 03:57

demo目录结构如下:


这里用到了lombok,在pom文件中引入依赖

<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.10</version></dependency>

在Intellij idea中快速构建Springboot web工程,

创建User类:

@Data@NoArgsConstructorpublic class User {    private String username;    private String password;    public User(String username, String password) {        this.username = username;        this.password = password;    }}
创建UserController类:

@RestControllerpublic class UserController {    @RequestMapping("/user")    public User helloUser(@RequestParam(value="username",defaultValue = "tomcmd") String username,                          @RequestParam(value = "password",defaultValue = "123456") String password){        return new User(username,password);    }}

启动类代码,这里不需要更改

@SpringBootApplicationpublic class Demo01Application {public static void main(String[] args) {SpringApplication.run(Demo01Application.class, args);}}
配置文件:application.properties中可以配置启动端口

server.port=8000
最后,启动工程,浏览器中访问localhost:8000/user?username=root&password=123,结果如下:





原创粉丝点击