首页 文章

使用GF4和Jackson找不到媒体类型= application / json的MessageBodyWriter

提问于
浏览
0

每次我尝试调用我的REST服务时,都会收到以下错误消息

[2016-09-01T16:27:37.782+0200] [Payara 4.1] [SEVERE] []   [org.glassfish.jersey.message.internal.WriterInterceptorExecutor] [tid: _ThreadID=28 _ThreadName=http-listener-1(3)] [timeMillis: 1472740057782] [levelValue: 1000] [[MessageBodyWriter not found for media type=application/json, type=class xxx.JsonClass, genericType=class xxx.JsonClass.]]

这是REST服务(剥离到相关部分):

import javax.ejb.EJB;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;


@Path("/service")
public class Service {

  @GET
  @Path("/callme")
  @Produces(MediaType.APPLICATION_JSON)
  public JsonClass callme(//
      @QueryParam("test") final String test, //
       ....) {
    return new JsonClass();
  }
}

JSON类

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

public class JsonClass {

  private String test;

  public JsonClass(final String test....) {
   ...
  }

  @JsonProperty
  public String getTest() {
    return this.test;
  }
}

POM.xml(有趣的部分)

<!-- DO NOT change the scope for jersey: https://java.net/jira/browse/JERSEY-1941 -->
<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
  <version>2.8</version>
  <scope>provided</scope>
</dependency>

我的设置是:

  • JDK8 / JEE7(build 1.8.0_51-b16)

  • Glassfish 4.1 Payara

  • Maven 3.2.5

这是我到目前为止所尝试的:

我仍然认为这是一个依赖问题 . 但是我想出了什么可能是问题 .

1 回答

  • 2

    不幸的是,我的上一篇文章被标记为重复,尽管问题和解决方案不同 . 因此,我发布了两个解决方案的新问题,希望能帮助您避免在桌面上敲打几个小时 .

    首选解决方案

    显然GF4附带了我不想使用的MoxyJson . 要集成您自己的依赖项 - 在我的情况下Jackson - 您需要使用以下代码禁用MoxyJson .

    @ApplicationPath("/")
    public class ApplicationConfig extends Application {
    
      /**
       * {@inheritDoc}
       */
      @Override
      public Map<String, Object> getProperties() {
        final Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("jersey.config.server.disableMoxyJson", true);
    
        return properties;
      }
    }
    

    然后添加自己的依赖项,例如在我的情况下只有那两个因为其他人被我使用的另一个lib引用 .

    <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-json-provider</artifactId>
      <version>2.6.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>2.6.2</version>
    </dependency>
    

    最后我犯了一个错误,即没有为@JsonProperty注释设置一个值,这将导致No MessageBodyWriter发现异常 . 为了避免这种情况,请使用以下ony相关的课程 .

    @JsonProperty("randomName")
    public String getRandomName(){
    ...
    }
    

    替代方案:

    比上面更糟糕的是你需要禁用MoxyJson,单独注册每个服务,并在使用GF的ResourceConfig时修复Bug .

    @ApplicationPath("/")
    public class ApplicationConfig extends ResourceConfig {
    
    /**
    * The default constructor.
    */
    public ApplicationConfig() {
    
    // Disable Moxy and use Jackson
    this.property(ServerProperties.MOXY_JSON_FEATURE_DISABLE, true);
    
    // Register own provider classes
    this.register(Fully.Qualified.Path.To.Your.Service.class);
    
    // Register Jackson provider
    // Workaround for GF4.1 bug for details: https://java.net/jira/browse/GLASSFISH-21141
    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JaxbAnnotationModule());
    this.register(new JacksonJaxbJsonProvider(mapper, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS));
    }
    }
    

    您需要ResourceConfig类的附加依赖项 .

    <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-json-provider</artifactId>
      <version>2.6.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>2.6.2</version>
    </dependency>
    
    <dependency>
      <groupId>org.glassfish.main.extras</groupId>
      <artifactId>glassfish-embedded-all</artifactId>
      <version>4.1.1</version>
      <scope>provided</scope>
    </dependency>
    

    最后与上面相同 - 请注意使用@JsonProperty设置值 .

相关问题