Java 通过@PostMapping分析对象,使用对方法的访问
我试图将请求主体解析为Ship对象Java 通过@PostMapping分析对象,使用对方法的访问,java,spring,json-deserialization,spring5,Java,Spring,Json Deserialization,Spring5,我试图将请求主体解析为Ship对象 @PostMapping("/ships") public Ship createShip(@RequestBody Ship ship){ return ship; } 当Ship对象被反序列化时,spring只对字段使用注入值。但是我希望spring为这个字段使用setter 我尝试将注释@JsonSetter添加到setter中,效果很好。但我认为这不是一个好办法 @Entity public
@PostMapping("/ships")
public Ship createShip(@RequestBody Ship ship){
return ship;
}
当Ship对象被反序列化时,spring只对字段使用注入值。但是我希望spring为这个字段使用setter
我尝试将注释@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)
或
将您的Ship类保持为POJO,setters中的验证条件可以组织为中提到的spring验证特性 使用spring验证bean-
“但我认为这是一种糟糕的方式。”为什么?这个注释的描述说,如果setter方法有一个非标准名称,那么它用来指示setter方法。但它并不是说它强迫我们使用这种方法,而不是直接进入这个领域。我不明白为什么它对我如此有效,它让我害怕“可能你不应该使用@Entity类作为请求/响应类”,创建一个具有相同字段的新类“ShipRequest”,并将其用作RequestBody类实际上,的文档中说:“可用于定义非静态、单参数方法作为逻辑属性“setter”的注释,作为推荐的
JsonProperty
注释的替代方法。”——因此您应该在getter方法中使用@JsonProperty
。我删除了实体并创建了POJO,但方法仍然没有使用:(谢谢你,我现在可以使用它了。但是有一个问题:我不能使用“spring验证”:(这是申请实习的任务,其中一个条件是不添加新的依赖项。验证是spring core的一部分,您不必在codein pom.xml中添加任何新的依赖项。我有依赖项:spring core、spring beans、spring context、spring aop、spring webmvc、spring data jpa、spring test,但我不能添加annotaTIONE Size或Max或其他。Idea要求添加依赖项javax。验证…可能我做错了什么?我用自定义验证更新了答案,以及如何在控制器中使用它。非常感谢!我不知道这是可能的。)
@Entity
@JsonDeserialize(access=METHODS)
public class Ship {
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);
}
}