0

I am working on:

Java web application
Jersey/rest
Java 8

I have a list of entities, and for each entity the user has a link to create a sub-entity on the server (the values for the sub entity are computed on the server and require no user input).

I have a web page with something like this:

Entity 1
<a href=".../entity/1/subentity">Create sub</a>

Entity 2
Sub Already Exists

Entity 3
<a href=".../entity/2/subentity">Create sub</a>

...

However, I think according to REST this should be a PUT/POST instead of a GET. Do I really need to replace all these links with a form wrapped around each one to get around this issue?

Is there a way in Jersey to convert these GETs to PUTs on the server the way you can with HttpMethodOverrideFilter for POSTS?

EDIT: This web application will not publicly available.

Andy Cribbens
  • 1,370
  • 2
  • 11
  • 22
  • Write your own filter. But BEWARE OF CRAWLERS! – Paul Samsotha Jan 15 '17 at 16:35
  • 1
    Using a POST instead of a GET has good, technical and semantic reasons: browsers and proxies are not supposed to retry such requests because they're not idempotent, crawlers are not supposed to submit post forms, proxies are not supposed to cache the responses, etc. "Transforming" a POST into a GET at server-side doesn't make much sense, and defeats the purpose of using a POST. – JB Nizet Jan 15 '17 at 16:56
  • Thanks JB Nizet, just to clarify I was considering transforming a GET to a PUT, just in case that makes any difference. – Andy Cribbens Jan 15 '17 at 17:02
  • If you request consists in creating something, and is not idempotent, a POST is the right method. – JB Nizet Jan 15 '17 at 18:30
  • It is creating something but it is non-idempotent so I am leaning toward PUT. – Andy Cribbens Jan 15 '17 at 20:23
  • So the solution I am now considering is to use links, however clicking the links would submit a form (as per this answer http://stackoverflow.com/questions/37583932/how-to-convert-a-get-request-to-post) which is dynamically generated so as not to clutter the html with a ton of forms. Then using `HttpMethodOverrideFilter` I can override this POST to become PUT. – Andy Cribbens Jan 15 '17 at 20:26
  • If the requests are sent as GETs through the network, there is no sense to convert them to PUT or POST on server. The fact if it's converted to a PUT or POST on the server won't make any difference. – pcjuzer Jan 16 '17 at 08:51
  • Thanks @pcjuzer . In this approach the request would be sent as a POST through the network (not GET) because of the JavaScript. The given that forms don't support PUT I think its fairly common to convert to PUT using `HttpMethodOverrideFilter` – Andy Cribbens Jan 16 '17 at 09:18
  • I've read in Javadoc that HttpMethodOverrideFilter is for cases when proxies don't let in PUT/DELETE method. Additionally, doesn't this filter require a special HTTP header, like X-HTTP-Method-Override? In your case, wouldn't it be fine to simply use @POST JAX-RS annotation on the endpoint? – pcjuzer Jan 16 '17 at 15:59
  • @POST would do I think, but PUT describes it slightly better in my situation. I think you have two approaches and the header is one. The other is just to append ?_method=PUT to the URL which strikes me as easier. – Andy Cribbens Jan 16 '17 at 16:49

0 Answers0