Home > Blockchain >  Spring 2.6.3 how to return id in response
Spring 2.6.3 how to return id in response

Time:02-06

Im making a api with spring, in which I return an array of objects, each of them contains name and an array. The problem is that it doesn't return the Id of each object which I store in jpa repository. The question is how do I turn Id visible in response

@Column
private @Id @GeneratedValue Long id;
private String QuestionName;

CodePudding user response:

Good evening,

Some more information about how you are structuring your project should be in order. However, I am going to assume your code snippet is from a class using the @Entity annotation.

If so, you require a Class noted with @RestController, where a @PostMapping function would be defined where you would receive your information from the client (Possibly in a JSON format, which can be directly converted into a Map by the application). By using a function with a return format of ResponseEntity, you can return HTTP messages to the client. In this return statement, you may return whatever you please, meaning you may return a JSON back to the client in question.

Personally, I recommend creating a QuestionDTO class (Data Transfer Object), in order to not send the Object with all Server-Side information to the client-side, easing creation of client-side code too!

I know this was a lot to take in, I leave some sources down here:

Response Entities and how they work

Building REST applications with Spring Boot

I hope this was of use, hit me up if you need further assistance,

Have a good one!

CodePudding user response:

Add getter and setter methods for id field and then try.

  •  Tags:  
  • Related