Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Header in jersey from a GET request

From a js page (in angular) I call a REST request, GET method, were I would to pass an header, this is the function that I call from the REST request:

        allstaffworking: function(_getstaff){
            var currentToken = _GetToken();

            var Headers = {
                token: currentToken.stringtoken
            };

            console.log("idtoken"+Headers);

            if (currentToken !== null) {
            $http({  
                        method : 'GET',  
                        headers: Headers,
                        url : REST_URL+'staff/working'
                    }).then(function successCallback(response) {  
                        _getstaff(response)
                    }, function errorCallback(response) {  
                        console.log(response.statusText);  
                    });  
               }  else {
                console.log("NON SEI LOGGATO!!!");
            }
        },

Whithout headers: Headers, it works, but I want to pass an important json string: {"idtokenStaff":11,"staffType":{"idstaffType":2,"type":"Dipendente"},"tokenStaff":"88d08m8ve4n8i71k796vajkd01"} in the Headers. I don't know How I can take this string in Jersey. This is java file in with I have the REST method:

 @Path("/staff")  
public class StaffController {  

BaseDao sDao =  new StaffDaoImpl();
StaffDao stfDao =  new StaffDaoImpl();
TokenStaffDao tsDao = new TokenStaffDaoImpl();
TokenStaff ts = new TokenStaff();

    @GET  
    @Produces(MediaType.APPLICATION_JSON)  
 public List<Staff> getStaff()  
 {  

  List<Staff> listOfStaff=sDao.getAll(Staff.class);
  return listOfStaff;  
 }  

    @GET  
    @Path("/working")  
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes("application/json")
    public List<Staff> getWStaff(@HeaderParam("token") String token) throws JSONException  
 {  

        JSONObject jsonObj = new JSONObject(token);

    Boolean id = tsDao.getExistence(jsonObj.getInt("idtokenStaff"));
    if (id){
        List<Staff> listOfWStaff=stfDao.getAllW();
        return listOfWStaff;  
    }
    else
        return null;
 }
}

Taking header from: @HeaderParam("token") String token. How Can I take the element of the header?

like image 615
Alfonso Silvestri Avatar asked Dec 19 '22 11:12

Alfonso Silvestri


1 Answers

A bit late to answer this, but you can also use @Context annotation to get httpheaders. Eg.

public List<Staff> getWStaff(@Context HttpHeaders httpHeaders) {
   String token = httpHeaders.getHeaderString("token");
    JSONObject jsonObj = new JSONObject(token);
}
like image 102
priteshbaviskar Avatar answered Jan 03 '23 05:01

priteshbaviskar