0

I'm kinda new with spring rest api. I have two entities that have unidirectional many to one relationship.

@Entity
public class Users{
    @Id @Column(name = "user_id") @JsonProperty("userId")
    private int id;

    @ManyToOne @JoinColumn("city_id")
    private City city;

    // other fields, getters, setters
}

@Entity
public class City{
    @Id @Column(name = "city_id") @JsonProperty("cityId")
    private int id;

    private String name;

    // other fields, getters, setters
}

Suppose that I already have some cities in city table. When I want to add new user with city id 2 using http post method, I had to do something like this:

{
  "userId": 1,
  "city": {
     "cityId": 2
  }
}

As you can see I had to group cityId inside city entity first. How can I do it without grouping it? like this :

{
  "userId": 1,
  "cityId": 2
}
kucinghitam
  • 414
  • 3
  • 12

1 Answers1

0

You can use @JsonUnwrapped link

Annotation used to indicate that a property should be serialized "unwrapped"; that is, if it would be serialized as JSON Object, its properties are instead included as properties of its containing Object.

sample code:

@Entity
public class Users{
    @Id @Column(name = "user_id") @JsonProperty("userId")
    private int id;

    @JsonUnwrapped
    @ManyToOne @JoinColumn("city_id")
    private City city;

    // other fields, getters, setters
}
Royts
  • 501
  • 6
  • 14
  • It works and has successfully saved the data to database, but strangely I also got this message error `Could not write JSON: Can not write a number, expecting field name (context: Object); nested exception is com.fasterxml.jackson.core.JsonGenerationException: Can not write a number, expecting field name (context: Object)` as it was still requires me to group it like before – kucinghitam Mar 15 '19 at 07:22
  • If it works, where is the error came from? I mean before or after you save it successfully? – Royts Mar 15 '19 at 07:23
  • I got the error when I sent http post request to add new user but when I checked the table manually on database, the new user I sent were already there saved. – kucinghitam Mar 15 '19 at 07:28
  • Also a new problem arise. Now it always gives me the same error message whenever I want to just retrieve the user information using GET method. – kucinghitam Mar 15 '19 at 07:39
  • Unfortunately, i cannot help you with the new error with just the error message.Maybe I would be able to help you if I can see your post endpoint. Based on error message, there's a problem on how you (pass/received?) the json data. – Royts Mar 15 '19 at 08:25
  • Also, @JsonUnwrapped has nothing to do with deserialization of json, so I don't think that it has something to do with your get method error. – Royts Mar 15 '19 at 08:26
  • Nevermind. I used `@JsonIdentityInfo` annotation too before along with `@JsonUnwrapped` which was apparently causing conflict. It works fine after I deleted the former. Also, I added `@JsonIgnoreProperties` suggested from [this](https://stackoverflow.com/questions/17542240/how-to-serialize-only-the-id-of-a-child-with-jackson) to serialize only the city id. – kucinghitam Mar 15 '19 at 09:28
  • Okay. Glad to help! – Royts Mar 15 '19 at 09:31