【问题标题】:MessageBodyWriter not found for media type=application/json ,added Jakson dependency still issue persisitMessageBodyWriter not found for media type=application/json ,添加杰克逊依赖仍然存在问题
【发布时间】:2016-09-12 08:47:17
【问题描述】:

我正在创建 restful web 服务,我正在尝试获取 json 响应,但未找到消息编写器异常。在 Jersey 站点中,有人提到对应用程序/JSON 使用 Jakson 依赖项,所以我使用它仍然得到相同的错误。对于响应类型 application/xml 它工作正常。 这些是 POM 中可用的依赖项,仍然出现 Message writer not found 异常。任何人都可以帮忙 -

 <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
            <version>2.2</version>
        </dependency>
public class UserServiceTest extends JerseyTest {

@Override
 protected Application configure(){
    ResourceConfig resourceConfig=new ResourceConfig(UserService.class);
     return resourceConfig;
 }
@Test
public void test(){
    String output=target("/users/test").request().get(String.class);
    System.out.println(output);
    //assertEquals("abc",output);
    Assert.assertEquals("abc",output);
}

}

【问题讨论】:

  • 您需要在您的应用程序中注册JacksonFeature。这仅在 2.9 之前需要
  • @peeskillet 感谢您的评论。请建议如何做到这一点?我使用了 jettison。它给出了同样的错误
  • 显示您的应用配置,无论是 web.xml 还是 Java 配置

标签: json web-services rest maven pom.xml


【解决方案1】:

在 Jersey 2.9 之前,您需要注册 JacksonFeature(之后会自动注册)。在您的情况下,只需使用 ResourceConfig

注册即可
resourceConfig.register(JacksonFeature.class)

ResourceConfig 仅用于配置服务器端。对于客户端,您可以注册该功能,如

@Override
public void configureClient(ClientConfig config) {
    config.register(JacksonFeature.class)l
}

在您的JerseyTest

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 2015-05-24
    相关资源
    最近更新 更多