Java 在多部分web服务中使用JAXB
我正在编写Jersey客户端和服务器,使用multipart/mix发送请求。每个多部分bodypart都是一个Java类。使用JAXB在客户端封送对象,在服务器端取消封送Java 在多部分web服务中使用JAXB,java,xml,jaxb,multipart,Java,Xml,Jaxb,Multipart,我正在编写Jersey客户端和服务器,使用multipart/mix发送请求。每个多部分bodypart都是一个Java类。使用JAXB在客户端封送对象,在服务器端取消封送 封送处理后,打印输出的是xml文件,但在发送到服务器后,为什么打印为com.path.data.io呢。BBody@2b8dd61d? 我用whileshark检查,xml被发送过来了 答:因为多部分已将其解组到对象 在服务器端获取此错误 javax.xml.bind.UnmarshalException 除此之外: [o
- 除此之外: [org.xml.sax.SAXParseException;行号:1;列号:1;prolog中不允许包含内容。]
@XmlRootElement(name="bBody")
@XmlAccessorType(XmlAccessType.FIELD)
public class BBody {
@XmlAttribute
String a;
@XmlElement
String b;
public BBody() {}
public void setA(String a) {
this.a = a;
}
客户端代码
public ClientResponse multikey(CommonRequest request)
throws Exception {
String server = request.getURL();
URI path = URI.create(server).resolve("/" + multikey);
Client client = Client.create();
WebResource rRoot = client.resource(path);
WebResource.Builder builder = rRoot.header("Date", date);
// Construct a MultiPart with each operation as a body part
MultiPart multiPart = new MultiPart();
for (BBody bBody : request.getBBodyList()) {
JAXBContext ctx = JAXBContext.newInstance(BBody.class);
StringWriter writer = new StringWriter();
ctx.createMarshaller().marshal(bBody, writer);
String custString = writer.toString();
_log.info ("custString is {}", custString);
// here print custString is <?xml version="1.0" encoding="UTF-8" standalone="yes"?><bBody a="2c741c38-f120"><b>foo</b></bBody>
multiPart.bodyPart(new BodyPart(custString, MediaType.APPLICATION_XML_TYPE));
}
ClientResponse result = builder.type(MultiPartMediaTypes.createMixed()).post(ClientResponse.class, multiPart)
return new result;
@POST
@Path("{ignore: .+}")
@Consumes("multipart/mixed")
public Response bulk(MultiPart multiPart,
@Context HttpServletRequest request,
@Context HttpServletResponse response) throws SuspendExecution {
for (BodyPart bodyPart : multiPart.getBodyParts()) {
log.info("bodypart entity dump {}", bodyPart.getEntity());
//no need extra unmashal
BBody bBody = bodyPart.getEntityAs(BBody.class)
}
}
在不深入研究您的问题的情况下,
如何处理需要封送到xml的类中的InputStream类型代码>毫无意义。InputStream是字节流。如何通过网络传递流?溪流是正在进行的东西。基于这个问题,在对基本原理的理解上存在着巨大的差距。在解决一个大问题之前,你需要采取很多措施来理解基础。谢谢克里斯托弗,我对这个问题真的很新,你有什么建议来建立基础吗?就像任何一本书一样。通过搜索问题,总是在这里和那里找到一块,很难全面地学习。对不起,我没有读过任何关于这方面的书。我要说的是,在编组/解编组时,目标是传递简单的数据。这是字符串、整数等。这些数据可以封装在Java类中,但该类应该只是一个带有getter/setter的简单对象。类似于InputStream
的东西是一个复杂的对象,它不仅仅是getter/setter。好的,谢谢!在我的代码中,InputStream实际上是一个请求内容,不应该是InputStream类型。除此之外,我的另外两个问题非常简单,因为类只有字符串。您是否介意快速查看一下?是否添加了功能?