我使用的是一个restful web服务,CRUD操作在其中工作,除了在一个页面上列出每个用户之外。getUser()方法仅用于登录到Web应用程序。我已经研究过这个问题,但我不使用命名查询。
我得到的错误::
严重:产生媒体类型冲突。资源方法公共…UserResource.getUser()和。。。UserResource.list()引发org.codehaus.jackson.JsonGenerationException,org.codehaus.jackson.map.JsonMappingException,java.io.IOException可以产生相同的介质类型
UserResource.list()
@GET
@Produces(MediaType.APPLICATION_JSON)
public String list() throws JsonGenerationException, JsonMappingException, IOException {
this.logger.info("list()");
ObjectWriter viewWriter;
if (this.isAdmin()) {
viewWriter = this.mapper.writerWithView(JsonViews.Admin.class);
} else {
viewWriter = this.mapper.writerWithView(JsonViews.User.class);
}
List<User> allEntries = this.userDao.findAll();
return viewWriter.writeValueAsString(allEntries);
}
UserResource.getUser()
/**
* Retrieves the currently logged in user.
*
* @return A transfer containing the username and the roles.
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public UserTransfer getUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Object principal = authentication.getPrincipal();
if (principal instanceof String && ((String) principal).equals("anonymousUser")) {
throw new WebApplicationException(401);
}
UserDetails userDetails = (UserDetails) principal;
return new UserTransfer(userDetails.getUsername(), this.createRoleMap(userDetails));
}
提前感谢,
您的资源位于同一路径,当Jersey需要选择一种方法时,没有任何区别(它们具有相同的HTTP方法、相同的路径、相同的媒体类型)。错误是关于媒体类型的,因为完全有可能在同一路径上有两个方法和HTTP方法,只是使用不同的媒体类型。这使他们区别于
@GET
@Produces(MediaType.APPLICATION_XML)
public String list();
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getUser();
但这可能不是你想要的。因此,解决方案是只更改其中一个上的路径
@GET
@Produces(MediaType.APPLICATION_JSON)
public String list();
@GET
@Path("/loggedInUser")
@Produces(MediaType.APPLICATION_JSON)
public String getUser();