问题
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!
回答1:
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);
来源:https://stackoverflow.com/questions/43756435/send-params-in-header-and-get-them-in-server-side-java