在尝试在Spring REST控制器中返回对象时出现HTTP状态406 – 不可接受的错误。

huangapple go评论74阅读模式
英文:

Getting HTTP Status 406 – Not Acceptable error when trying to return an object in Spring REST controller

问题

这里我正在尝试在Spring REST控制器中更新一个对象,并以JSON格式返回更新后的对象。

控制器:

@RequestMapping(value = "/user/revert", method = RequestMethod.POST)
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
public ResponseEntity<UserResponse> revertUserData(@RequestParam(value = "userName") String userName) {

    User user = new User();
    UserResponse userResponse = new UserResponse();

    try {
        user = userService.findUserByName(userName);
        user.setLastName(null);

        userResponse.setEmail(user.getEmail());
        userResponse.setLastName(user.getLastName());

    } catch (Exception e) {
        return new ResponseEntity<UserResponse>(userResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }
    return new ResponseEntity<UserResponse>(userResponse, HttpStatus.OK);
}

UserResponse类:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserResponse {

  @JsonProperty("email")
  private String email;
  @JsonProperty("lastName")
  private String lastName;

  //getter and setter methods 

}

pom文件:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1</version>
</dependency>

出现的错误:

目标资源没有当前表示,该表示根据请求中接收到的主动协商头字段不被用户代理接受,并且服务器不愿意提供默认表示。
英文:

Here i'm trying to update an object and return the updated object in JSON format in a Spring REST controller.

Controller :

@RequestMapping(value = &quot;/user/revert&quot;, method = RequestMethod.POST)
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
public ResponseEntity&lt;UserResponse&gt; revertUserData(@RequestParam(value = &quot;userName&quot;) String userName) {

    User user = new User();
    UserResponse userResponse = new UserResponse();

    try {
        user = userService.findUserByName(userName);
        user.setLastName(null);

        userResponse.setEmail(user.getEmail());
        userResponse.setLastName(user.getLastName());

    } catch (Exception e) {
        return new ResponseEntity&lt;UserResponse&gt;(userResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }
    return new ResponseEntity&lt;UserResponse&gt;(userResponse, HttpStatus.OK);

}

UserResponse class :

@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserResponse {

  @JsonProperty(&quot;email&quot;)
  private String email;
  @JsonProperty(&quot;lastName&quot;)
  private String lastName;

  //getter and setter methids 

}

pom file :

    &lt;dependency&gt;
        &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
        &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
        &lt;version&gt;2.4.1&lt;/version&gt;
    &lt;/dependency&gt;

The error im getting :

The target resource does not have a current representation that would be acceptable to the
user agent, according to the proactive negotiation header fields received in the request, and the server is
unwilling to supply a default representation.

答案1

得分: 1

你混合了JAX-RS和Spring MVC的注解:@RequestMapping来自Spring,@Produces来自JAX-RS。如果你查看一下@RequestMapping文档,你会看到它有一个produces参数。所以你应该有类似这样的代码:

@RequestMapping(value = "/user/revert", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
public ResponseEntity<UserResponse> revertUserData(@RequestParam(value = "userName") String userName){
...
}
英文:

You mixed JAX-RS and Spring MVC annotations: @RequestMapping comes from Spring and @Produces from JAX-RS. If you take a look at the @RequestMapping's documentation you'll see that it has a produces parameter. So you should have something like:

@RequestMapping(value = &quot;/user/revert&quot;, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
public ResponseEntity&lt;UserResponse&gt; revertUserData(@RequestParam(value = &quot;userName&quot;) String userName){
...
}

huangapple
  • 本文由 发表于 2020年10月7日 19:44:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64243320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定