2

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?

Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
Henrik Voß
  • 126
  • 1
  • 8
  • possible duplicate of [Jersey version issue: MessageBodyReader not found for media type=application/xml](http://stackoverflow.com/questions/30754641/jersey-version-issue-messagebodyreader-not-found-for-media-type-application-xml) – Paul Samsotha Jun 12 '15 at 17:42

1 Answers1

6

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

Community
  • 1
  • 1
Henrik Voß
  • 126
  • 1
  • 8