Jersey Response.seeOther() not giving HTTP 303 status code

大憨熊 提交于 2020-02-05 07:32:27

问题


I have a social networking site in which we have a restricted community.

When a guest user applies requests for membership, a membership request is generated. I am performing this task through a REST API.

I am using RESTClient plugin in firefox to send requests.

When I send a POST resquest for adding self to the restricted community, the response is the memebership request generated for the same. Hence, the underlying HTTP status code is 303 See Other POST/GET.

I get proper response for the same with the membership request in response but the status code is 200 OK(which I think is the code for the GET on the membership request).

But, I need the same to be 303. I have tried debugging also. My finding is that the Jersey Response.seeOther() is not returning 303.

Please help.

The code snippets are as follows :

addUserToCommunity method :

MembershipRequestModel membershipRequest = null;
membershipRequest = communityService.addUserToCommunity(communityId, userId);
if(membershipRequest != null) {
  // Add code 303 if returning membershiprequest
  return seeOther( membershipRequest, 
                   String.valueOf(membershipRequest.getId()), 
                   MembershipRequestRestHandlerImpl.class);
} else {
  return ok(null);
}

seeOther method which in turn calls Response.seeOther() :

public Response seeOther(Object model, String id, Class<?> resource){

  URI location = buildLocation(resource, id);

  return Response.seeOther(location).entity(createResponseItems(model)).build();
}

回答1:


303 - See Other is essentially a redirect - browser would automatically follow the URL provided in location header. Are you sure this is not what you see? I.e. maybe the firefox plugin you are using is simply following the redirect (behind the scenes) and returning the result of a GET method on that location URL. Try FireBug which shows you exact requests that went on the wire - you should be able to see the redirect there.




回答2:


If you want to geht the 303 instead of the 200 Response:

httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);

The redirect URL ist in the Location: header




回答3:


This is actually a problem with RESTClient i.e; the browser as Martin says.

The browser actually performs the redirect and does not give 303.

Instead try hitting the URI using cURL in the shell or using puTTY. It works fine.

Thank you.



来源:https://stackoverflow.com/questions/9905336/jersey-response-seeother-not-giving-http-303-status-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!