Jersey/Tomcat - 产生媒体冲突

Jersey/Tomcat - Producing media conflict

我正在使用 restful Web 服务,除了在一个页面上列出每个用户外,CRUD 操作在其中运行。 getUser() 方法仅用于登录到 webapp。我已经看过 this 问题,但我没有使用命名查询。

我收到的错误::

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

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 需要 select 一个方法时,没有什么可以区分它们(它们有相同的 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();