Jersy 2 Client + JAXB (MessageBodyWriter not found)

大城市里の小女人 提交于 2019-12-11 06:57:14

问题


I'm trying to use Jersy 2 in client mode to post XML to a server but i always get an exception.

I have got only one dependency in my pom file:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.18</version>
</dependency>

My Java code:

public static void main(String... args) {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8080");
    Entity<SimpleClass> entity = Entity.entity(new SimpleClass(), MediaType.APPLICATION_XML_TYPE);
    target.request(MediaType.TEXT_XML_TYPE).post(entity);
}

@XmlRootElement(name = "test")
@XmlAccessorType(XmlAccessType.NONE)
public class SimpleClass {
    @XmlElement(name = "hello")
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

Exception:

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/xml, type=class jersey.SimpleClass, genericType=class jersey.SimpleClass.

What I'm doing wrong?


回答1:


Thank's to peeskillet!

Since Jersey 2.16 you have to add JAX-B support:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>2.18</version>
</dependency>

See: Jersey version issue: MessageBodyReader not found for media type=application/xml



来源:https://stackoverflow.com/questions/30809083/jersy-2-client-jaxb-messagebodywriter-not-found

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