在头部发送参数并将它们放到服务器端 - java [英] Send params in header and get them in server side - java

查看:155
本文介绍了在头部发送参数并将它们放到服务器端 - java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的java服务器端代码:

  @GET 
@Path(/ usersForMobile)
@Produces({MediaType.APPLICATION_JSON +; charset = utf-8 })
public Response getUsersForMobile(@Context UriInfo info){
String rashutId;
字符串用户名;
字符串密码;
列表<用户> inspectorList = new ArrayList< User>();

尝试{

rashutId = info.getQueryParameters()。getFirst(rashutId);
userName = info.getQueryParameters()。getFirst(userName);
password = info.getQueryParameters()。getFirst(password);
inspectorList = LoginService.getInspectorsList(userName,password,rashutId);

} catch(Exception e){
// TODO自动生成的catch块
e.printStackTrace(); (!inspectorList.isEmpty())
返回Response.status(Status.OK).entity(new UsersResponse(inspectorList,true))。build() ;
else
return Response.status(Status.OK).entity(No inspectors found)。build();


$ b $ / code>

到现在为止, url,但它不是可靠的方式,所以我决定在头文件中传递它们,我怎么才能从请求头获得它们?



谢谢!

解决方案

您可以使用@HeaderParam(paramName)来获取HTTPHeader参数,例如

  public Response getUsersForMobile(@Context UriInfo info,@HeaderParam(paramName)String paramValue)

或者使用你的上下文

  String paramValue = info.getRequestHeader(paramName)获得(0); 


I'm using Jersey and I want to get params from the header of request;

Here is my java server side code:

@GET
@Path("/usersForMobile")
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response getUsersForMobile(@Context UriInfo info) {  
    String rashutId ;
    String userName ;
    String password;
    List<User> inspectorList= new ArrayList<User>();

    try {

         rashutId = info.getQueryParameters().getFirst("rashutId");
         userName = info.getQueryParameters().getFirst("userName");
         password = info.getQueryParameters().getFirst("password");
         inspectorList = LoginService.getInspectorsList(userName,password,rashutId);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();        
        }

        if(!inspectorList.isEmpty())
            return Response.status(Status.OK).entity(new UsersResponse(inspectorList, true)).build();
        else
            return Response.status(Status.OK).entity("No inspectors found").build();

    }
}

Until now I got the params from url, but it's not reliable way, so I decided to pass them in the header, how can I get them from request header?

thanks!

解决方案

Either you can use an @HeaderParam("paramName") to get the HTTPHeader param e.g

public Response getUsersForMobile(@Context UriInfo info, @HeaderParam("paramName")String  paramValue) 

or use the context you have

String paramValue = info.getRequestHeader("paramName").get(0);

这篇关于在头部发送参数并将它们放到服务器端 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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