简体   繁体   English

泽西岛/ Tomcat-产生媒体冲突

[英]Jersey/Tomcat - Producing media conflict

I am using a restful web service in which CRUD operations work, except for listing every single user on one page. 我正在使用一个平稳的Web服务,其中可以进行CRUD操作,除了在一页上列出每个用户之外。 The getUser() method is only used for logging into the webapp. getUser()方法仅用于登录Web应用程序。 I already took a look at this question, but I am not using named queries. 我已经看过这个问题,但是我没有使用命名查询。

The error I am getting:: 我得到的错误:

SEVERE: Producing media type conflict. 严重:产生媒体类型冲突。 The resource methods public ...UserResource.getUser() and ...UserResource.list() throws org.codehaus.jackson.JsonGenerationException,org.codehaus.jackson.map.JsonMappingException,java.io.IOException can produce the same media type 资源方法public ... UserResource.getUser()和... UserResource.list()抛出org.codehaus.jackson.JsonGenerationException,org.codehaus.jackson.map.JsonMappingException,java.io.IOException可以产生相同的媒体类型

UserResource.list() 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() 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));
}

Thanks in advance, 提前致谢,

Your resources are to the same path, and there is nothing to differentiate them when Jersey needs to select a method (they have the same HTTP method, same path, same media type). 您的资源位于同一路径,当Jersey需要选择一种方法(它们具有相同的HTTP方法,相同的路径,相同的媒体类型)时,没有什么可区分它们的。 The error is about media types, because it is completely possible to have two methods on the same path and HTTP method, just with different media types. 该错误与媒体类型有关,因为完全有可能在同一路径和HTTP方法上使用两种方法,只是媒体类型不同。 This differentiates them 这使他们与众不同

@GET
@Produces(MediaType.APPLICATION_XML)
public String list();

@GET
@Produces(MediaType.APPLICATION_JSON)
public String getUser();

But this it probably not what you want. 但这可能不是您想要的。 So the solution is to just change the path on one of them 因此,解决方案是仅更改其中之一的路径

@GET
@Produces(MediaType.APPLICATION_JSON)
public String list();

@GET
@Path("/loggedInUser")
@Produces(MediaType.APPLICATION_JSON)
public String getUser();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM