如何访问 HTTP 请求? [英] How do I access the HTTP request?

查看:19
本文介绍了如何访问 HTTP 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常说我在 Java 中有一个 REST 方法

Say normally I have a REST method in Java

@POST 
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public String showTime(@FormParam("username") String userName) {

:
:
:
}

这很好.但是,我想知道有没有一种方法可以使用 Jersey 访问完整的 HTTP 请求,例如

which is fine. However, I'm wondering is there a way I can access the full HTTP request with Jersey such as

@POST 
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public String showTime(@FormParam("username") String userName,@XXXXXX String httpRequest) {

:
:
:
}

其中一些注释会给我完整的 HTTP 请求以存储在变量中.我尝试过使用@POST,但它似乎不起作用.有什么建议吗?

where some annotation would give me the full HTTP request to store in a variable. I have tried using @POST but it doesn't seem to work. Any suggestions?

推荐答案

可以使用@Context注解:

You can use the @Context annotation:

@POST 
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String showTime(
    @FormParam("username") String userName,
    @Context HttpServletRequest httpRequest
) {
    // The method body
}

这篇关于如何访问 HTTP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆