我试图在客户端模式下使用Jersy 2将XML发布到服务器但我总是遇到异常。
我的pom文件中只有一个依赖项:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.18</version>
</dependency>
我的Java代码:
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 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.
我做错了什么?
答案 0 :(得分:5)
感谢peeskillet!
自Jersey 2.16起,您必须添加JAX-B支持:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>2.18</version>
</dependency>
请参阅: Jersey version issue: MessageBodyReader not found for media type=application/xml