POST 带 HeaderParam 注释的球衣

POST jersey with HeaderParam annotation

当我发送 POST 时,我得到空值。请有人知道如何在 Jersey 中使用 @HeaderParam 通过 Angular 传递值?

我在后端有这个:

 @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Users loginUser(@HeaderParam("username")String username,@HeaderParam("password") String password){
        System.out.println("What happen"+userService.loginService(username, password));
       return userService.loginService(username, password);
    }

这在前端,使用 Angular:

 $scope.getUserFunction = function () {
      $http({
        method  : 'POST',
        url     : 'http://localhost:8080/JersyBackEnd/resources/login'+data,
        contentType:'application/json',
        data    : {username: "akram",password:"yes"},
      })
        .then(function(data) {
          console.log(data);
        });
    };

由于空数据,我得到 204。
谁能帮助我,在 Jersey 中使用 HeaderParam、PathParam 或 FormParm 注释?

data 作为请求正文发送。你真正想要的是 headers.
From the docs:

  • data{string|Object} – Data to be sent as the request message data.
  • headers{Object} – Map of strings or functions which return strings representing HTTP headers to send to the server. If the return value of a function is null, the header will not be sent. Functions accept a config object as an argument.

示例:

$http({
  ...
  headers: {
    username: "akram",
    password: "yes"
  }
})

当您使用 @HeaderParam 时,您必须使用 headers: { 'something': 'anything' } 从 .JS 传递值。正如我目前看到的,您正在使用 data : {username: "akram",password:"yes"}。尝试使用 headers: { 'something': 'anything' } 而不是这个。

请尝试header,希望它能解决您的空问题。