Send params in header and get them in server side - java

▼魔方 西西 提交于 2019-12-08 09:50:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!