0
点赞
收藏
分享

微信扫一扫

springboot中对输入参数做验证,输出做处理

青鸾惊鸿 2022-03-16 阅读 68

添加maven配置:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
package com.fen.dou.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fen.dou.entity.UserReq;
import com.fen.dou.entity.UserRsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
public class InOutProcessController {
    @Autowired
    ApplicationContext context;
    @PostMapping("processParam")
    public void queryUser(@Valid @RequestBody UserReq user) throws JsonProcessingException {
        HttpMessageConverters converters = context.getBean(HttpMessageConverters.class);
        ObjectMapper objectMapper = new ObjectMapper();
        String rspStr = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
        System.out.println(rspStr);
        UserRsp rsp = objectMapper.readValue(rspStr, UserRsp.class);
        System.out.println(rsp.toString());
    }
}

 这里需要注意,spring-web中默认的序列化和反序列化是使用jackjson

package com.fen.dou.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Data
public class UserReq {

    @NotBlank(message = "姓名不能为空")
    private String name;

    @NotNull(message = "年龄不能为空")
    private Integer age;
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date birthday;
    private String school;
    @Range(max = 3, message = "名称最长支持3字符")
    private Integer height;
    @Max(value = 3, message = "名称最长支持3字符")
    private Integer yc_weight;
}

springboot-validation支持的注解有:
@AssertFalse    被注释的元素必须为 false
@AssertTrue     被注释的元素必须为 true
@DecimalMin(value)    被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value)    被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Digits (integer, fraction)    被注释的元素必须是一个数字,其值必须在可接受的范围内
@Email    被注释的元素必须是电子邮箱地址
@Future    被注释的元素必须是一个将来的日期
@Length(min=,max=)    被注释的字符串的大小必须在指定的范围内
@Min(value)    被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value)    被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Negative    该值必须小于0
@NegativeOrZero    该值必须小于等于0
@Null    被注释的元素必须为 null
@NotNull    被注释的元素必须不为 null
@NotBlank(message =)    验证字符串非null,且长度必须大于0
@NotEmpty    被注释的字符串的必须非空
@Past    被注释的元素必须是一个过去的日期
@Pattern(regex=,flag=)    被注释的元素必须符合指定的正则表达式
@Positive    该值必须大于0
@PositiveOrZero    该值必须大于等于0
@Range(min=,max=,message=)    被注释的元素必须在合适的范围内
@Size(max=, min=)    数组大小必须在[min,max]这个区间
@URL(protocol=,host,port)    检查是否是一个有效的URL,如果提供了protocol,host等,则该URL还需满足提供的条件
@Valid    该注解主要用于字段为一个包含其他对象的集合或map或数组的字段,或该字段直接为一个其他对象的引用,这样在检查当前对象的同时也会检查该字段所引用的对象

要注意某些注解需针对数据类型,如@Length修饰的是字符串类型

在输出参数中,我们常见的处理是对时间进行格式化处理和对字段名转换,这里可以用spring-web自带的插件jackjson来进行处理

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
public class UserRsp {

    private String name;
    private Integer age;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date birthday;
    private String school;
    private Integer height;

    @JsonProperty(value  = "yc_weight", access = JsonProperty.Access.READ_WRITE)
    private Integer weight;
}
举报

相关推荐

0 条评论