rest - With Spring Boot, how to post JSON object that represents an entity whose member is another entity -


(*** edit *: ** I'm looking for the JSON representation of the solution given here :)

I have the following entity account in which the member of another entity is a member variable

@Entity public class account {@Id @ generatedValue (strategy = GenerationType.IDENTITY) private Long id = 0; @ManyToOne @JoinColumn (name = "customer_id") private customer client; ...} @Entity Public Class Customer {@Id @GeneratedValue (strategy = GenerationType.IDENTITY) Private long ID = 0; Private string client ID; Public Client () {} Public Customer (String Client ID) {this.customerId = customerId; } Public long IID (return) {return ID; } Public string getCustomerId () {return customerId; } Public Zero Set Customer ID (String Client Id) {this.customerId = customerId; }}

I need to represent the JSON object of an account. Here is the controller method:

  @ Autonomous Personal Account Resource Accounts; @RequestMapping (value = "/ account / account", method = RequestMethod.POST) Add public @ResponseBody account account (@RequestBody account account) {return accounts.save (account); }  

Account Részatari is an interface that extends crude repository

I have a URI http: // localhost: 8084 / customer / customer / 1 Is an existing customer with

I have tried to post the following JSON object and received a 400 bad request response. {"Customer": "1"} {"Customer": "Http: // localhost: 8084 / Customer / Customer / 1"} {"Customer": {"href": "Http: // localhost: 8084 / customer / customer / 1"}}

To ensure that an account can be created when the correct data is received, I modified the controller method according to which I created the account while posting at http: // localhost: 8084 / account / account

  @RequestMapping (value = "/ Account / account", method = RequestMethod.POST) public @Respon SeBody Add AccountAuthority () throws exceptions {customer customer = customers.findOne (1); Account account = new account (); Account.setCustomer (customer); Return accounts. Save (account); }  

So my question is, how can I format a JSON object so that one unit can be formed whose members are the current entity?

I found that the correct format is to provide the URI for the referenced unit, that is, in this case it should be

{"Customer": "http: // localhost: 8084 / customer / customer / 1"}

However, this only data-rest dependency Works only after adding

gt; pre & lt; & Lt; Group & gt; Org.springframework.boot & lt; / Group & gt; & Lt; ArtifactId & gt; Spring-boot starter-data-rest & lt; / ArtifactId> & Lt; Version & gt; {Spri} & lt; / Edition & gt; & Lt; / Dependencies & gt;

Without this dependency, the request fails with JsonMappingException


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -