Подтвердить что ты не робот

Получение доступа к объекту HttpServletRequest в спокойной веб-службе

Я могу получить доступ к объекту запроса HttpServlet в мыльной веб-службе следующим образом: Объявление частного поля для WebServiceContext в реализации службы и аннотирование его как ресурса:

@Resource
private WebServiceContext context;

Чтобы получить объект HttpServletRequet, я пишу код, как показано ниже:

MessageContext ctx = context.getMessageContext();
HttpServletRequest request =(HttpServletRequest)ctx.get(AbstractHTTPDestination.HTTP_REQUEST);

Но эти вещи не работают в спокойной веб-службе. Я использую Apache CXF для создания спокойного веб-сервиса. Скажите, как я могу получить доступ к объекту HttpServletRequest.

4b9b3361

Ответ 1

Я бы рекомендовал использовать org.apache.cxf.jaxrs.ext.MessageContext

import javax.ws.rs.core.Context;
import org.apache.cxf.jaxrs.ext.MessageContext;

...
// add the attribute to your implementation
@Context 
private MessageContext context;

...
// then you can access the request/response/session etc in your methods
HttpServletRequest req = context.getHttpServletRequest();
HttpServletResponse res = context.getHttpServletResponse()

Вы можете использовать аннотацию @Context для обозначения других типов (например, ServletContext или HttpServletRequest). См. Контекстные аннотации.

Ответ 2

используйте этот код для запроса доступа и ответа для каждого запроса:

@Path("/User")
public class RestClass{

    @GET
    @Path("/getUserInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getUserrDetails(@Context HttpServletRequest request,
            @Context HttpServletResponse response) {
        String username = request.getParameter("txt_username");
        String password = request.getParameter("txt_password");
        System.out.println(username);
        System.out.println(password);

        User user = new User(username, password);

        return Response.ok().status(200).entity(user).build();
    }
... 
}