• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use DTO instead of Query params in POST  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to figure this one out it's rather unfamiliar territory. I am using Jersey what I dont understand is how do I actually get the values listed into the DTO file I created. The code shown below right now currently when using a post request I am able to extract the values out successfully But instead of being present in the URI I want them in the JSON Body  essentially I want to use the dto instead of the query params heres what I have so far..


SETTER AND GETTER



METHOD IN OTHER FILE










 
Saloon Keeper
Posts: 8913
167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Use the DTO class itself as the parameter of your action, and annotate it with @FormParam.

Unrelated to your question, you're using the wrong HTTP verb. If you want to overwrite a resource with a certain ID, you should be using PUT:
 
maur smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. Unfortunately Apache is spewing out a lot of error messages. I have took your code into consideration and applied that with no success. I should have been a little bit more descriptive I guess when writing my post;

I am using Mailman to send a POST it is in fact a POST request I need to perform. I noticed you didn't include the @Consumes annotation so is this not required in a POST request? Just to recap I'm trying to use the DTO file instead of the query params. If you dont mind as well after taking that into consideration can you explain a little more about what's going on here and why you chose what so I may understand better? thanks
 
Sheriff
Posts: 21331
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Use the DTO class itself as the parameter of your action, and annotate it with @FormParam.


How does that work, converting form parameter "dog" to a Dog instance?

It's real easy with JSON, as long as the browser sends actual JSON:

Note how the Dog attribute is not annotated; the JAX-RS implementation (Jersey, RestEasy, whatever) should now try to parse the request body as JSON (thanks to the @Consume annotation) into an instance of Dog. So as long as the browser sends JSON in the following format, that should be fine:
 
Stephan van Hulst
Saloon Keeper
Posts: 8913
167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recall that @FormParam can be used to specify which property from the JSON object in the request body you want to use. For instance, I *think* my code above should work with a request like this:
 
Rob Spoor
Sheriff
Posts: 21331
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, I'd prefer the explicit JSON consumption.
 
maur smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize for a tardy follow up.. day to day busi-ness!The dynamics and scope have changed a bit or maybe my understanding of this particular piece. I have experimented with the @BeanParam annotation and it seems to do a lot of the heavy lifting I was passing it in like this, after importing the class ofcourse      Dogs(@BeanParam Dogs dogs) {}  and giving it the {"id"} as per posted.. heres the thing I had this working and was prepared to post the code to offer community members my solution but the individual is asking if we could do away with @BeanParam entirely. Now I should elaborate  little bit and give greater context. When this POST is performed it is tied with other files or DTO's all following POJO style these classes or DTO's all have fields in their particular classes and extend and pass generics with each other in one way or another I think that is enough context ? because of how popular that design pattern is.. back to my question; if the person does not want those values being populated by @QuereyParams or POST URL specifically how else can one manipulate the state of these booleans in my Dog class? This is perplexing to me and is honestly the first time i'm working on backend stuff  following DTO and POJO style. On the client side in typescript I have the exact DTO's which  is how everything connects and offers the GET solution.

  I'm going to attempt to illustrate a little what's going on although if it's not clear please let me know..


**POSTMAN*** - Send out POST request website/token=XseseeionId=X


DTO's In backend***  DTOS  --- Dog.java  -> Result.java -> Response.java -> Service.java -> Base.java

NOTE: Each DTO class has it's own fields, setters, getters etc. and return value to the caller in a separate file.


Objective: Change POST from Query Params to use DTO's







 
maur smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone who encounters this in the future ( sorry long wait in posted solution ) Essentially when you git this path lets say /Home    You can actually just feed the class in the params which will consume it. Doing this in POJO style would look something  like  this




Was as simple as that but I was over looking it with the annotations and other Java stuff, what helped was reading docs and knowing how to do this afterwards but more importantly understand why for the next time!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!