gpt4 book ai didi

java - 使用 Jersey 发布到服务 - "A message body writer ... was not found"

转载 作者:行者123 更新时间:2023-12-01 14:28:10 25 4
gpt4 key购买 nike

我已经使用 Java/Jersey 编写了一个基本的 RESTful 服务来管理订阅者,现在我正在尝试创建一个客户端来与该服务通信,但我遇到了一个我不明白的运行时错误。这是一个显示问题的精简版本:

订阅者类别:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Subscriber {

private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Subscriber() {
}

}

主要测试应用:

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class MyTestClient {

public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080/MyService").build());

Subscriber s = new Subscriber() {{
setFirstName("John");
setLastName("Doe");
}};

System.out.println(service.path("subscriber")
.type(MediaType.APPLICATION_XML)
.entity(s)
.post(String.class));
}
}

我收到此错误:

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class MyTestClient$1, and MIME media type, application/xml, was not found

我不清楚这个错误消息的确切含义;它似乎与订阅者到 XML 的转换有关(尽管对我来说,错误消息意味着它正在尝试转换 MyTestClient,这不可能是正确的......)我在我的服务,创建 XML 发送给客户端没有问题,所以我很困惑。

如果我用包含 XML 的字符串替换订阅者对象,我不会收到错误消息,但出于多种原因我想使用对象。

此错误消息的原因是什么?如何解决?

编辑:作为引用,虽然我不确定它是否相关,但这里是代码服务端的相关部分:

@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Subscriber post(Subscriber subscriber) {
/// doesn't get here
}

此外,这可以工作,但不使用订阅者对象:

    String xml = "<subscriber><firstName>John</firstName><lastName>Doe</lastName></subscriber>";

System.out.println(service.path("subscriber")
.type(MediaType.APPLICATION_XML)
.entity(xml)
.post(String.class));

更新:我可以通过首先将对象显式转换为字符串来解决该问题,因此:

JAXBContext context = JAXBContext.newInstance(Subscriber.class);
Marshaller m = context.createMarshaller();
StringWriter sw = new StringWriter();
m.marshal(s, sw);
String xml = sw.toString();

System.out.println(service.path("subscriber")
.type(MediaType.APPLICATION_XML)
.entity(xml)
.post(String.class));

但这相当困惑,我不明白为什么有必要。

最佳答案

错误“Java 类型的消息正文编写器,类 MyTestClient$1”报告它正在尝试编码匿名内部类。我们通常期望看到“类订阅者”。在执行此 new Subscriber() {{}} 时,您可能会丢失 JAXB 注释。

我的第一个建议是将名称传递给构造函数,这样会更干净,并且不会让您丢失 JAXB 注释。

Subscriber s = new Subscriber("John","Doe");

或者调用默认构造函数,然后设置字段:

Subscriber s = new Subscriber();
s.setFirstName("John");
s.setLastName("Doe");

如果您需要定义匿名内部类,那么您可以尝试在新的匿名类定义之前重新注释@XmlRootElement。 (虽然从未尝试过,所以您的里程可能会有所不同:)

关于java - 使用 Jersey 发布到服务 - "A message body writer ... was not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17023766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com