简体   繁体   中英

Jersey: How to redirect/forward WITHOUT URL changing in address bar

I am using Jerysey's implementation of JAX-RS to write a REST API. My POJO that handles the get requests eventually forwards it to a JSP. Everything is working fine, the problem is that the forwarding causes the URL in the browser's address bar to change to the URL that the request was forwarded to. How do I do a redirect WITHOUT this URL changing in the address bar? Current, I have tried 4 different ways:

return Response.seeOther(uri).build(),

return Response.temporaryRedirect(uri),

//thrown exception: throw new WebApplicationException(response),

return Response.status(303).location(uri).build();

It doesn't sound like a Jersey issue per se. Jersey is doing its part to receive the request, do some processing, and return the response you are expecting.

It sounds more like a servlet container issue. Why don't you want the url to change in the browser?

Restful services can (and should) be built with no concern about templates/JSPs/consumers. Take a look at a library like RestAssured, write some tests for your work, and you will see that it is acting as expected.

Instead of rendering out to a JSP, consider using a rest client to make straight http requests against your service.

If you want the url to remain unchanged, consider making the http call using an AJAX library (JQuery or other Javascript-based solution).

I hope that helps!

A RESTful resource is identified by a URL. So if you redirect to another resource with another URL the URL in the address bar should change. That's good because you can eg bookmark this URL or send it per eMail.

The question here is are you really redirecting to another resource or do you only want to return a different representation (HTML instead of eg JSON). If the latter you should not redirect. Let your resource-class directly return text/html by using Jerseys Viewables .

You could make the entire website inside an iFrame and load the new site into that frame. It will maintain the page URL and load your content.

http://www.w3schools.com/tags/tag_iframe.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM