0

我试图将请求正文解析为 Ship 对象

    @PostMapping("/ships")
    public Ship createShip(@RequestBody Ship ship){

        return ship;
    }

而当 Ship 对象被反序列化时,spring 只对字段使用注入值。但我希望这个领域的春天使用设置器。

我尝试将注释 @JsonSetter 添加到 setter 并且效果很好。但我认为这是不好的方式。

    @Entity
    public class Ship {

        @Id
        @GeneratedValue
        private Long id;

        private String name;
        private String planet;


        public String getName() {
            return name;
        }

        @JsonSetter
        public void setName(String name) {
            if(name == null || name == "") throw new IllegalArgumentException("Error while setting name. Can't be null and empty");
            if(name.length() > 50) throw new IllegalArgumentException("Error while setting name. Can't be mere than 50 chars");
            this.name = name;
        }

        public String getPlanet() {
            return planet;
        }

        @JsonSetter
        public void setPlanet(String planet) {
            if(planet == null || planet == "") throw new IllegalArgumentException("Error while setting planet. Can't be null and empty");
            if(planet.length() > 50) throw new IllegalArgumentException("Error while setting planet. Can't be mere than 50 chars");
            this.planet = planet;
        }

    }

可能存在一些这样的注释:

    createShip(@RequestBody(access = METHODS) Ship ship)

或者

    @Entity 
    @JsonDeserialize(access=METHODS)
    public class Ship {
4

1 回答 1

1

将您的 Ship 类保持为 POJO,并在 setter 中验证条件可以组织为spring 手册中提到的 spring 验证功能。

使用 spring 验证 bean -

  1. 定义自定义验证器
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;


@Component
public class ShipValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return Ship.class.equals(clazz);
    }

    @Override
    public void validate(Object obj, Errors errors) {
         Ship ship = (Ship) obj;
         String name = ship.getName();
         String planet = ship.getPlanet();

         if(StringUtils.isEmpty(name)) errors.rejectValue("name", "Can't be null or Empty");
         if(StringUtils.isEmpty(planet)) errors.rejectValue("planet", "Can't be null or Empty");
         if(name.length() > 50) errors.rejectValue( "name", "Can't be more than 50 chars");
         if(planet.length() > 50) errors.rejectValue("planet", "Can't be more than 50 chars");

    }

}

  1. 更改控制器以使用此验证器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class ShipController {

    @Autowired ShipRepo shipRepo;

    @Autowired ShipValidator shipValidator;

    @PostMapping("/ships")
    public Ship saveShip(@RequestBody Ship ship, BindingResult result) {
        shipValidator.validate(ship, result);

        if(result.hasErrors()) {
            //TODO: add your exception handling logic and handle these errors           
            throw new IllegalArgumentException("Error in properties");
        }

        return shipRepo.save(ship);
    }
}

于 2020-04-13T05:02:25.963 回答