1.spring boot的启动类必须在其他类的包名之上
2.SpringBoot响应客户端数据
@RestController
public class Hello {
@GetMapping("/getuser")
public User getUser() {
User u = new User();
u.setAge(100);
u.setName("wanqing");
u.setKey("imnu");
return u;
}
}
{"name":"wanqing","age":100,"key":"imnu"}
3.SpringBoot 使用devtools进行热部署
Maven.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Gradle.
dependencies {
compile("org.springframework.boot:spring-boot-devtools")
}
4.Spring Boot资源文件属性配置
配置属性resource.properties
com.imnu.asiainfo.name=wangqing
com.imnu.asiainfo.age=12
com.imnu.asiainfo.key=1231dfsf
配置entity
@Configuration
@ConfigurationProperties(prefix="com.imnu.asiainfo")//前缀
@PropertySource(value = "classpath:resource.properties")//引用文件
public class User {
private String name;
private int age;
private String key;
}
使用
@Autowired
private User user;
@GetMapping("/getuser")
public User getUser() {
User u = new User();
BeanUtils.copyProperties(user, u);
return u;
}
5.传递参数
<单值传入>
@RequestMapping(value= "/user",method = RequestMethod.GET)
public List<User> query(@RequestParam(name = "username",required=false,defaultValue="wangqing") String Lusername){
System.out.println(Lusername);
List<User> users = new ArrayList<>();
return users;
}
<多值传入>
@RequestMapping(value= "/user",method = RequestMethod.GET)
public List<User> query(User user){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE); --将对象反射(使用jar org.apache.commons)
System.out.println(l);
List<User> users = new ArrayList<>();
return users;
}
<分页>
@RequestMapping(value= "/user",method = RequestMethod.GET)
public List<User> query(User user,@PageableDefault(page=2,size=10,sort="name,asc") Pageable pageable){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
System.out.println(l);
return users;
}
6.参数绑定
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public User getInfo(@PathVariable String id) {
System.out.println("----------"+id);
User u= new User();
return u;
}
7.JsonView
使用接口来声明多个视图
在值对象的get方法上指定视图
在Controller方法上指定视图
public class User {
public interface UserSimpleView {};
public interface UserDetailView extends UserSimpleView {};
private String name;
private int age;
private String key;
@JsonView(UserSimpleView.class)
public String getName() {
return name;
}
@JsonView(UserDetailView.class)
public int getAge() {
return age;
}
@RequestMapping(value= "/user",method = RequestMethod.GET)
@JsonView(User.UserDetailView.class)
public List<User> query(User user){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
System.out.println(l);
List<User> users = new ArrayList<>();
users.add(user);
return users;
}
返回[{"name":"wangqing","age":12}]
@RequestMapping(value= "/user",method = RequestMethod.GET)
@JsonView(User.UserSimpleView.class)
public List<User> query(User user){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
System.out.println(l);
List<User> users = new ArrayList<>();
users.add(user);
return users;
}
返回 [{"name":"wangqing"}]
8.post后台数据绑定
@PostMapping("/create")
public User create(@RequestBody User user) {
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getKey());
return user;
}
9.校验参数 @Valid 的使用
public class User {
public interface UserSimpleView {};
public interface UserDetailView extends UserSimpleView {};
private String name;
private int age;
@NotBlank
private String key;
}
@PostMapping("/create")
public User create(@Valid @RequestBody User user) {
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getKey());
return user;
}
BindingResult错误信息输出
@PostMapping("/create")
public User create(@Valid @RequestBody User user ,BindingResult errors) {
if (errors.hasErrors()) {
errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
}
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getKey());
return user;
}
2.SpringBoot响应客户端数据
@RestController
public class Hello {
@GetMapping("/getuser")
public User getUser() {
User u = new User();
u.setAge(100);
u.setName("wanqing");
u.setKey("imnu");
return u;
}
}
{"name":"wanqing","age":100,"key":"imnu"}
3.SpringBoot 使用devtools进行热部署
Maven.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Gradle.
dependencies {
compile("org.springframework.boot:spring-boot-devtools")
}
4.Spring Boot资源文件属性配置
配置属性resource.properties
com.imnu.asiainfo.name=wangqing
com.imnu.asiainfo.age=12
com.imnu.asiainfo.key=1231dfsf
配置entity
@Configuration
@ConfigurationProperties(prefix="com.imnu.asiainfo")//前缀
@PropertySource(value = "classpath:resource.properties")//引用文件
public class User {
private String name;
private int age;
private String key;
}
使用
@Autowired
private User user;
@GetMapping("/getuser")
public User getUser() {
User u = new User();
BeanUtils.copyProperties(user, u);
return u;
}
5.传递参数
<单值传入>
@RequestMapping(value= "/user",method = RequestMethod.GET)
public List<User> query(@RequestParam(name = "username",required=false,defaultValue="wangqing") String Lusername){
System.out.println(Lusername);
List<User> users = new ArrayList<>();
return users;
}
<多值传入>
@RequestMapping(value= "/user",method = RequestMethod.GET)
public List<User> query(User user){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE); --将对象反射(使用jar org.apache.commons)
System.out.println(l);
List<User> users = new ArrayList<>();
return users;
}
<分页>
@RequestMapping(value= "/user",method = RequestMethod.GET)
public List<User> query(User user,@PageableDefault(page=2,size=10,sort="name,asc") Pageable pageable){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
System.out.println(l);
return users;
}
6.参数绑定
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public User getInfo(@PathVariable String id) {
System.out.println("----------"+id);
User u= new User();
return u;
}
7.JsonView
使用接口来声明多个视图
在值对象的get方法上指定视图
在Controller方法上指定视图
public class User {
public interface UserSimpleView {};
public interface UserDetailView extends UserSimpleView {};
private String name;
private int age;
private String key;
@JsonView(UserSimpleView.class)
public String getName() {
return name;
}
@JsonView(UserDetailView.class)
public int getAge() {
return age;
}
@RequestMapping(value= "/user",method = RequestMethod.GET)
@JsonView(User.UserDetailView.class)
public List<User> query(User user){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
System.out.println(l);
List<User> users = new ArrayList<>();
users.add(user);
return users;
}
返回[{"name":"wangqing","age":12}]
@RequestMapping(value= "/user",method = RequestMethod.GET)
@JsonView(User.UserSimpleView.class)
public List<User> query(User user){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
System.out.println(l);
List<User> users = new ArrayList<>();
users.add(user);
return users;
}
返回 [{"name":"wangqing"}]
8.post后台数据绑定
@PostMapping("/create")
public User create(@RequestBody User user) {
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getKey());
return user;
}
9.校验参数 @Valid 的使用
public class User {
public interface UserSimpleView {};
public interface UserDetailView extends UserSimpleView {};
private String name;
private int age;
@NotBlank
private String key;
}
@PostMapping("/create")
public User create(@Valid @RequestBody User user) {
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getKey());
return user;
}
BindingResult错误信息输出
@PostMapping("/create")
public User create(@Valid @RequestBody User user ,BindingResult errors) {
if (errors.hasErrors()) {
errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
}
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getKey());
return user;
}