如何解决未找到的MessageBodyWriter?

时间:2017-10-06 17:38:40

标签: java rest web-services tomcat7 media-type

错误: org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor aroundWriteToSEVERE:找不到媒体类型的MessageBodyWriter = application / vnd.xyzcompany.v1 + json,type = class model.OrderStatus,genericType = class model.OrderStatu

OrderStatus是一个模型类。

尝试返回OrderStatus类的Object时遇到此问题。

主要原因是Media Type,当我尝试使用 application / json 时,它有效,但需要使用自定义媒体类型 喜欢: application / vnd.xyzcompany.v1 + json

额外信息: @Produces和@Consumes使用相同的媒体类型,OrderStatus类在请求的响应中定义。

无论我在哪里阅读,大部分都发现,自定义mime /媒体类型需要注册,但不知道如何注册和实施。

任何人都可以帮忙解决此问题。

提前致谢:)

1 个答案:

答案 0 :(得分:0)

Using custom media/mime type and serializing/deserializing the received or sent object according to the new media type.

It just requires a class with "@Provider" annotation and a "@Produces". The produces annotation will be written as: @Produces({"application/customType.v1+json", "application/json"}).

With a constructor/method having Object mapper serialization and deserialization.

NOTE: Keep this class in the package where all API classes are.

Example Code:

@Provider
@Produces({MediaType.APPLICATION_JSON, "application/customType.v1+json"})
public class JacksonJsonProvider extends JacksonJaxbJsonProvider {

    public JacksonJsonProvider() {

        ObjectMapper objectMapper = new ObjectMapper()
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .registerModule(new JodaModule())
            .setDateFormat(new RFC3339DateFormat());

        setMapper(objectMapper);
    }
}