A message body writer for Java class … and MIME media type text/html was not found [duplicate]

别等时光非礼了梦想. 提交于 2019-12-18 04:38:14

问题


I am using the jms/atmosphere framework to make communication between two applications. One of the applications is a message producer for a topic, sending custom objects of the following type:

    @XmlRootElement
    public class A implements Serializable{
    public A(){}

    /* some private properties */

   }

On the other side more than one consumers are listening on the topic and make different subscriptions depending on the id.

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public SuspendResponse<A> subscribe() {
    return new SuspendResponse.SuspendResponseBuilder<A>()
            .broadcaster(topic)
            .outputComments(true)
            .addListener(new EventsLogger()).build();
    } 
   @Override
public void incomingBroadcast() {
    try {
        String id = getID();
        if (id.startsWith("/*")) {
            id = "atmosphere";
        }

        logger.info("Looking up Connection Factory {}", FACTORY_NAME);
        Context ctx = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup(FACTORY_NAME);

        logger.info("Looking up topic: {}", TOPIC_NAME);
        Topic topic = (Topic) ctx.lookup(TOPIC_NAME);

        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        logger.info("Create consumer for : {}", id);
        String selector = String.format("BroadcasterId = '%s'", id);

        consumer = session.createConsumer(topic, selector);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message msg) {
                try {
                    ObjectMessage om = (ObjectMessage) msg;
                    A a = (A) om.getObject();
                    if (a!= null && bc != null) {
                        broadcastReceivedMessage(a);
                    }
                    logger.info("Broadcasted message: {} ", a);
                } catch (JMSException ex) {
                    logger.warn("Failed to broadcast message", ex);
                }
            }
        });
        publisher = session.createProducer(topic);
        connection.start();
        logger.info("JMS created for topic {}, with filter {}", TOPIC_NAME, selector);
    } catch (Throwable ex) {
        throw new IllegalStateException("Unable to initialize MyBroadcaster", ex);
    }

}

What I notice is that the messages are arriving correctly on the JMS topic, but I receive the following exception:

   SEVERE: A message body writer for Java class A, and Java type class A, and MIME       
   media type text/html was not found

   SEVERE: The registered message body writers compatible with the MIME media type are:

   */* ->
   com.sun.jersey.core.impl.provider.entity.FormProvider
   com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
   com.sun.jersey.core.impl.provider.entity.StringProvider
   com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
   com.sun.jersey.core.impl.provider.entity.FileProvider
   com.sun.jersey.core.impl.provider.entity.InputStreamProvider
   com.sun.jersey.core.impl.provider.entity.DataSourceProvider
   com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
   com.sun.jersey.core.impl.provider.entity.ReaderProvider
   com.sun.jersey.core.impl.provider.entity.DocumentProvider
   com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
   com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
   com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
   com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
   com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
   com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvider
   com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
   com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
   com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
   com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
   com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
   com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy
   com.sun.jersey.moxy.MoxyMessageBodyWorker
   com.sun.jersey.moxy.MoxyListMessageBodyWorker

I am using Netbeans 7.0.1, glassfish 3.1.1, atmosphere 0.8.1, jersey 1.11. I searched the web an tried any possible solution but nothing helped.


回答1:


I had the same issue and it was down to not having Jersey's json module included on my classpath. You can simply fix it by adding the following dependency on maven

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.14</version>
</dependency>



回答2:


It seems that you need to implement the required MessageBodyWriter for the classes which your transport through Jersey.

You can attach @Provider to class A, and make it implements MessageBodyWriter such as:

@Provider public class A implements MessageBodyWriter

This will force you to override the required methods(writeTo, getSize, isWriteable) while jersey is tranporting the objects in your methods.



来源:https://stackoverflow.com/questions/8645674/a-message-body-writer-for-java-class-and-mime-media-type-text-html-was-not-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!