在头文件中发送params并在服务器端获取它们 – java

我正在使用泽西岛,我希望从请求标题中获取参数;

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

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

到目前为止,我从url获取了params,但这不是可靠的方式,所以我决定在标题中传递它们,如何从请求标头中获取它们?

谢谢!

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

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

或使用您拥有的上下文

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