简体   繁体   English

使用 Spring 引导请求包装器主体

[英]Request Wrapper Body using Spring Boot

I have two entities that represent two tables in the database, similar to the concept of inheritance:我有两个实体代表数据库中的两个表,类似于inheritance的概念:

Entity Person:实体人:

@Entity
@Table(name ="person", schema = "myself")
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="person_id")
    private long id;

    @Column(name = "person_name", nullable = false, length = 255)
    @NotNull(message = "name_expects")
    private String name;

    @OneToOne(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = false, fetch = FetchType.LAZY)
    @JsonIgnore
    private AdressPerson adressPerson;

... get and setters
}

Entity Adress:实体地址:

@Entity
@Table(name ="adress_person", schema = "myself")
public class AdressPerson implements Serializable {

    @Id
    @Column(name = "person_id")
    private long id;

    @Column(name = "type", nullable = false, length = 14)
    private String type;

    @Column(name = "adress", nullable = false, length = 255)
    private String adress;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private Person person;

... get and setters
}

I need to send a single request post and form-data with this:我需要用这个发送一个请求帖子和表单数据:

{
   "id":1,
   "name":"John Doe",
   "type": "home",
   "adress" : "Madison Ave"
}

In my controller I need to receive this way:在我的 controller 我需要这样接收:

...
@PostMapping(value = "", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
public AdressPerson addAdressPerson(@NotNull @RequestParam AdressPerson adressPersonRequest)  {
    //code
}
...

However I am not able to do the mapping, I have tried to use filter and RequestWrapper, but I was not able to overwrite the message body.但是我无法进行映射,我尝试使用过滤器和 RequestWrapper,但我无法覆盖消息正文。 Anyone can help me?任何人都可以帮助我吗?

Thanks!谢谢!

Spring's @RequestParam maps query parameters, like ?name=something&address=some-address Spring 的@RequestParam映射查询参数,例如?name=something&address=some-address

While you're trying to make a POST request and want to deserialize JSON request payload into a java class.当您尝试发出 POST 请求并希望将 JSON 请求有效负载反序列化为 java class 时。

Use @RequestBody instead.请改用@RequestBody

Within your @RestController class you can define your POST mapping method as follows:在您的 @RestController class 中,您可以定义您的 POST 映射方法,如下所示:

...
@PostMapping(value = "")
public AdressPerson addPerson(@RequestBody Person person)  {
       //code
}
...

As a default, your spring-boot project expects / returns a JSON body, so you can omit the consumes and produces attributes.默认情况下,您的 spring-boot 项目预期/返回 JSON 主体,因此您可以省略消费和生产属性。

Now for receiving the payload in above controller, your JSON request body should look like -现在为了接收上面 controller 中的有效负载,您的 JSON 请求正文应如下所示 -

{
  "id":1,
  "name":"John Doe",
  adressPerson": {
      "type": "home",
      "adress" : "Madison Ave"
   }
}

Also, within your entity Person, remove @JsonIgnore from adressPerson field, instead try using @JsonManagedReference and @JsonBackReference.此外,在您的实体 Person 中,从 adressPerson 字段中删除 @JsonIgnore,而是尝试使用 @JsonManagedReference 和 @JsonBackReference。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM