Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB,REST将XML对象发送到远程URL_Java_Xml_Web Services_Rest_Jaxb - Fatal编程技术网

Java JAXB,REST将XML对象发送到远程URL

Java JAXB,REST将XML对象发送到远程URL,java,xml,web-services,rest,jaxb,Java,Xml,Web Services,Rest,Jaxb,外面似乎有很多版本。这就是我正在做的。我有一个RESTful Web服务,我正在尝试将Java对象打包到JAXB中,并通过连接将其发送到Web服务。这是我的客户端代码 private static void performPost(JAXBObject obj) { String url ="http://www.example.com/test?xml="; HttpURLConnection urlConnection = (HttpURLConnection) n

外面似乎有很多版本。这就是我正在做的。我有一个RESTful Web服务,我正在尝试将Java对象打包到JAXB中,并通过连接将其发送到Web服务。这是我的客户端代码

 private static void performPost(JAXBObject obj) {
     String url ="http://www.example.com/test?xml=";


     HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
     urlConnection.setRequestMethod("POST");
     urlConnection.setDoOutput(true);
     urlConnection.setRequestProperty("accept-charset", charset);
     urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
     urlConnection.setRequestProperty("accept", jsonContentType);

     JAXBContext jaxbContext = JAXBContext.newInstance(JAXBObject.class);
     Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
     jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
     jaxbMarshaller.marshal(obj, urlConnection.getOutputStream());  
     int rspCode = urlConnection.getResponseCode();
     ...
 }
Web服务代码是

@POST   
@Produces("application/json")
@Path("test")
public String doIt(@QueryParam("xml") String str) {
    int ret = doSomething(str);
    return new Gson().toJson(ret);
}
程序正在到达doIt,但是字符串str中没有任何内容。
我做错了什么?

您问题中的代码将XML作为消息发送,但试图将其作为查询参数接收。如果要将XML作为消息发送,则str参数不应使用@QueryParam进行注释。查询参数是URL中在?i、 例如,limit是查询参数。您正在尝试捕获不需要注释的XML消息体。

问题中的代码将XML作为消息发送,但尝试将其作为查询参数接收。如果要将XML作为消息发送,则str参数不应使用@QueryParam进行注释。查询参数是URL中在?i、 例如,limit是查询参数。您正在尝试捕获不需要注释的XML消息正文。

您实际在哪里设置请求正文?封送处理程序不设置正文吗?i、 e.jaxbMarshaller.marshallobj,urlConnection.getOutputStream@user2689782-是的封送处理操作设置了正文,您遇到的问题是在哪里尝试检索XML:您实际在哪里设置请求正文?封送处理不设置正文吗?i、 e.jaxbMarshaller.marshallobj,urlConnection.getOutputStream@user2689782-是的,封送处理操作设置了正文,您遇到的问题是在哪里尝试检索XML:哦,我明白了,所以我不能发送封送,以便可以传递多个XML文件?一个xml文件的一个输出流,我想…@user2689782-你想在一个请求中发送多个xml文件吗?是的,我想我必须找到解决方法。非常感谢你的帮助!哦,我明白了,所以我不能发送一个这样我就可以传递多个xml文件了?一个xml文件的一个输出流,我想…@user2689782-你想在一个请求中发送多个xml文件吗?是的,我想我必须找到解决方法。非常感谢你的帮助!