发表于: 2019-10-21 20:27:53
1 996
今天完成的事情:
@ResponseBody返回json对象
SpringMVC中使用@ResponseBody注解将任意POJO对象返回值转换成json进行返回
作用:该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
先引入maven依赖
<!-- 引入json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
作用是吧java对象转换成json对象
新建一个controller
import com.pojo.Student;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author admin
*/
@Controller
public class JsonController {
@Autowired
StudentService studentService;
@ResponseBody
@RequestMapping(value = "/list",method = RequestMethod.GET)
public List<Student> queryAllStudent(){
List<Student>students = studentService.queryAllStudent();
return students;
}
}
测试
postman演示
参数校验
引入需要得依赖
<!-- 参数校验依赖-->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.7.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.0.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.3.1</version>
</dependency>
在实体类用上参数校验注解
public class Student {
@NotNull(message="id不能为空!")
private int studentId;
@NotBlank(message="用户名不能为空!")
@Size(min=4,max=12,message="用户名的长度在4~12之间!")
private String studentName;
@NotNull(message="年龄不能为空!")
private int studentAge;
@NotBlank(message="账号不能为空!")
@Size(min=4,max=12,message="账号的长度在4~12之间!")
private String userName;
@NotBlank(message="密码不能为空!")
@Size(min=4,max=12,message="密码的长度在4~12之间!")
private String studentPassWord;
@Past
private Timestamp studentCreateTime;
@Future
private Timestamp studentUpdateTime;
新建一个Controller
*@Validated注解表示开启Spring的校验机制,支持分组校验,声明在入参上。
*@Valid注解表示开启Hibernate的校验机制,不支持分组校验,声明在入参上。
*在DTO后面紧跟BindingResult对象,那么当参数不符合时,能通过该对象直接获取不符合校验的message描述信息。
*若使用了@Validated/@Valid注解开启校验,但DTO后面没有紧跟BindingResult对象,那么当参数不符合时,将直接返回400 Bad Request状态码。
import com.pojo.Student;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author admin
*/
@RestController
public class BaseController {
@RequestMapping(value = "/test",method = RequestMethod.GET)
public Student test(@Validated Student student, BindingResult result){
if (result.hasErrors()){
List<ObjectError> errors = result.getAllErrors();
for (ObjectError error : errors){
System.out.println(error.getDefaultMessage());
}
}
return student;
}
}
测试结果
postman演示
明天计划的事情:争取完成任务二
遇到的问题:参数校验的注解使用错误
使用hibernate validator出现上面的错误,需要注意
@NotNull 和 @NotEmpty 和@NotBlank 区别
@NotEmpty 用在集合类上面
@NotBlank 用在String上面
@NotNull 用在基本类型上
在枚举类上不要加非空注解
收获:
完成responsebody返回json数据
完成参数校验
spring 参数校验注解
1.@validated 注解
@Validated is org.springframework.validation.annotation.Validated.
2.java validation 注解
JSR提供的校验注解:
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(regex=,flag=) 被注释的元素必须符合指定的正则表达式
3.hiberate validation 注解
@Email 被注释的元素必须是电子邮箱地址
@Length(min=,max=) 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range(min=,max=,message=) 被注释的元素必须在合适的范围内
@URL(protocol=,
host=, port=,
regexp=, flags=) 合法的url
主要区分下@NotNull @NotEmpty @NotBlank 3个注解的区别:
@NotNull 任何对象的value不能为null
@NotEmpty 集合对象的元素不为0,即集合不为空,也可以用于字符串不为null
@NotBlank 只能用于字符串不为null,并且字符串trim()以后length要大于0
评论