繁体   English   中英

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

[英]Jersey/Tomcat - Producing media conflict

我正在使用一个平稳的Web服务,其中可以进行CRUD操作,除了在一页上列出每个用户之外。 getUser()方法仅用于登录Web应用程序。 我已经看过这个问题,但是我没有使用命名查询。

我得到的错误:

严重:产生媒体类型冲突。 资源方法public ... 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();

暂无
暂无

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

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