您的位置:首页 > 编程语言 > Java开发

Springboot快速构建restful接口

2017-11-13 15:25 561 查看
demo目录结构如下:



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

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


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

创建User类:

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

@RestController
public 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);
}
}


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

@SpringBootApplication
public 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,结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: