• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to implement a REST update operation with JAX-RS?  RSS feed

 
Vasilis Souvatzis
Ranch Hand
Posts: 94
1
Chrome Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's my understanding that the PUT method is tricky to implement, because it relies completely on the business logic and what we're doing with the update anyway. I'm pretty sure that the Java EE 7 tutorial don't show a PUT implementation, and from what I've searched online every tutorial implements the operation differently. I'd really appreciate any help because I'm trying to distance myself from JSF + non-REST constantly, I'm going for the REST + JS-frontend this time.

My main approach is to delegate all operations on a DAO (the one I use directly in JSF beans) via injection. The following method in the PersonRepository works:

it adds the measurement, updates the datatable and all is well. Now that I'm trying to create the REST API, I'm not sure how to approach this. I've got the other methods completed, but not this. My initial thought is the following:

Could this work? Should perhaps the PersonRepository return a Person instead of being void? Should I be returning the URI of the person? Perhaps the method shouldn't have a Measurement argument but instead I should create it from the JSON consumed?
 
Stephan van Hulst
Saloon Keeper
Posts: 7743
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should design REST controllers the same way you would any other class, except that there are some standard routes you should support most of the time.

Route nameDescription
indexReturns all instances in the resource.
createAdds a new instance to the resource.
getFinds an existing instance in the resource.
updateUpdates an existing instance in the resource.
deleteRemoves an existing instance from the resource.

Note that you can use any name you want, I just prefer these.

Let's say that in this example, your measurements are temperatures:
 
Vasilis Souvatzis
Ranch Hand
Posts: 94
1
Chrome Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woooow this is better than the tutorials I've seen online! I really appreciate the help Stephen! Code is kept and will be assimilated, will have it as reference

From a quick glance I see you always return something on all methods, makes sense since you'll probably do something with it after the operation; show the updated resource or navigate to the created one.

Great, this gives me ideas about the front-end as well. I kind of see how the client should behave...

In any case, I'll work on my API and report back! Thanks again!
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both update and delete can either return a 200 with the object that was just updated/deleted, or a 204 with no content. In the latter case you can simply return void. You can use either, it's all just personal preference.

Create calls should preferably not return a 200, but instead a 201, with a Location header with the URL you can use to retrieve the newly created object. For instance:

The created URI is relative to the current one, and will be used for the Location header.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!