通过@PostMapping 解析对象,使用对方法的访问
Parsing Object by @PostMapping, using access to methods
我尝试将请求正文解析为 Ship 对象
@PostMapping("/ships")
public Ship createShip(@RequestBody Ship ship){
return ship;
}
并且当 Ship 对象被反序列化时,spring 仅对字段使用注入值。但我希望 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 {
将您的 Ship class 保留为 POJO,并且可以将设置器中的验证条件组织为 spring spring manual 中提到的验证功能。
正在使用 spring -
验证 bean
- 定义自定义验证器
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");
}
}
- 更改控制器以使用此验证器
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);
}
}
我尝试将请求正文解析为 Ship 对象
@PostMapping("/ships")
public Ship createShip(@RequestBody Ship ship){
return ship;
}
并且当 Ship 对象被反序列化时,spring 仅对字段使用注入值。但我希望 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 {
将您的 Ship class 保留为 POJO,并且可以将设置器中的验证条件组织为 spring spring manual 中提到的验证功能。
正在使用 spring -
验证 bean- 定义自定义验证器
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");
}
}
- 更改控制器以使用此验证器
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);
}
}