我使用Hibernate,除了这种情况,一切都很好。如果我有类和继承类,当我在Spring Controller中使用JSON发送类似的对象时,总是得到错误400 BadRequest。
如果我发送一个不是子类的对象,所有字段都会很好地传播,并且所有字段都存储得很好。 这是一个例外:
@Entity
@javax.persistence.Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person{
@Id
@GeneratedValue
@Column(name="id_person")
protected int id_person;
.....
@Entity
@javax.persistence.Table(name="client")
@PrimaryKeyJoinColumn(name="id_person")
public class Client extends Person{
@Column(name="address")
protected String address;
....
@Entity
@javax.persistence.Table(name="individual")
@PrimaryKeyJoinColumn(name="id_person")
public class Individual extends Client {
@Column(name="personal_number")
protected String personalNumber;
.....
我的方法inController:
@RequestMapping(value="/individualPerson", method=RequestMethod.POST)
public ResponseEntity<List<Individual>> posttest(HttpServletRequest request, HttpServletResponse response, @RequestBody Individual indiv)
发送继承的对象时是否需要一些额外的注释或设置?
此外,Hibernate会自动使用在这些类中注释的主键创建所有表。
答案 0 :(得分:0)
如果您使用JSON格式,默认情况下Jackson library不需要任何特殊注释。 Spring支持开箱即用,因此通常您不需要指定任何其他内容:
@RequestMapping(value="/individualPerson", method=RequestMethod.POST, produces="application/json")
其他选项是依赖于Spring支持的HTTP内容协商,并且不在produces
属性中指定任何格式
答案 1 :(得分:0)
如果您的路径中有Jackson
库,则可以进行两项更改:
@ResponseBody
注释。produces
上添加@RequestMapping
属性设置为org.springframework.http.MediaType.APPLICATION_JSON