英文:
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 = "/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 class :
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserResponse {
@JsonProperty("email")
private String email;
@JsonProperty("lastName")
private String lastName;
//getter and setter methids
}
pom file :
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
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 = "/user/revert", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
public ResponseEntity<UserResponse> revertUserData(@RequestParam(value = "userName") String userName){
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论