我需要将收到的json
对象转换为自定义实体类型Product
并进行验证。之后我需要坚持下去。有没有一种方法可以使用批注和Spring框架支持REST风格的Web服务,只用注释和控制器的方法签名来完成它?有没有办法将自定义对象转换为POST JSON后在方法签名中进行验证?
控制器
@RestController
@RequestMapping("/")
public class pController {
@RequestMapping(value = "/addProduct", method = RequestMethod.POST)
@ResponseBody
public String addProduct(Model model, @Valid Product product ,
BindingResult result){
// somehow get converted json as Product, should be already validated
}
实体
@Table(name = "product")
@Entity
public class Product implements Serializable {
@Id
@Column(name = "product")
@GeneratedValue
private int product;
@Column(name = "name")
@Basic
@NotEmpty
private String name;
...
}
你想要在控制器处理程序方法体中保留'Product'实例的东西吗? – 2014-09-20 17:51:42
我已经有服务来坚持产品。我只是想要验证的产品存储。 – 2014-09-20 18:17:38
啊,然后看下面的答案。 '@ RequestBody'来反序列化JSON(和其他类型)。 – 2014-09-20 18:18:23