在Java client中对restful webservice发送http post请求,将得到的请求转化为java pojo。
restful ws返回信息格式为
那么为了将这个xml转化为POJO,需要先创建这个POJO类:
[color=red]只需要在定义类的前面注明@XmlRootElement(name = "RequestPhysicalInventoryPollingMessage")[/color]
如果在每个属性前面加@XmlAttribute,则会报错。
因为英语很烂,不知道用英语写的注释有没有写错。。。中文重新说明下:
RequestPhysicalInventoryPollingMessage message = (RequestPhysicalInventoryPollingMessage) um
.unmarshal(is);
这个方法:unmarshal() ,他不能接受String类型的参数(个人很不理解,为什么jaxb不去实现简单的把xml写在String中的POJO转化)。所以如果现在有一个String,内容是xml,就必须先调用这个String的getBytes()方法,然后用得到的byte[]去创建一个ByteArrayInputStream。然后把这个InputStream作为参数调用unmarshal().
执行代码结果如下:
restful ws返回信息格式为
<RequestPhysicalInventoryPollingMessage>
<interval>int</interval>
<from>String</from>
<to>String</to>
<pollingurl>
String
</pollingurl>
</RequestPhysicalInventoryPollingMessage>
那么为了将这个xml转化为POJO,需要先创建这个POJO类:
package com.client.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "RequestPhysicalInventoryPollingMessage")
public class RequestPhysicalInventoryPollingMessage {
String interval;
String from;
String to;
String pollingurl;
public RequestPhysicalInventoryPollingMessage() {
interval = "c";
from = "a";
to = "b";
}
public String getInterval() {
return interval;
}
public void setInterval(String interval) {
this.interval = interval;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getPollingurl() {
return pollingurl;
}
public void setPollingurl(String pollingurl) {
this.pollingurl = pollingurl;
}
}
[color=red]只需要在定义类的前面注明@XmlRootElement(name = "RequestPhysicalInventoryPollingMessage")[/color]
如果在每个属性前面加@XmlAttribute,则会报错。
package com.client.jaxb;
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import com.client.RequestPhysicalInventoryPollingMessage;
public final class Client {
private Client() {
}
public static void main(String args[]) throws Exception {
//read the request payload
File input = new File(
"/RequestPhysicalInventory_FLOW_PIPeR_MPI3.1.xml");
//set the uri which we will send request and the http method is post
PostMethod post = new PostMethod(
"http://10.10.27.74:7001/ManagePhysicalInventoryConsumerProject/ManagePhysicalInventoryConsumerProxyService ");
post.addRequestHeader("Accept", "text/xml");
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
//execute the post method and get the response status code
int result = httpclient.executeMethod(post);
System.out.println("Response status code: " + result);
System.out.println("Response body: ");
//print the body of response
System.out.println(post.getResponseBodyAsString());
//invoke the method to convert the xml to a pojo
RequestPhysicalInventoryPollingMessage message = xml2pojo(post
.getResponseBodyAsString());
System.out.println("interval:" + message.getInterval());
System.out.println("from:" + message.getFrom());
System.out.println("to:" + message.getTo());
System.out.println("pollinguri:" + message.getPollingurl());
}
//convert the xml to a pojo
public static RequestPhysicalInventoryPollingMessage xml2pojo(String xml)
throws JAXBException {
JAXBContext jaxbContext;
//set the pojo in a new instance of JaxbContext
jaxbContext = JAXBContext
.newInstance(RequestPhysicalInventoryPollingMessage.class);
Unmarshaller um = jaxbContext.createUnmarshaller();
ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
/*
* this method has no parameter as String,so we must get the bytes from a String,and create a ByteArrayInputStream
* by the bytes.
*/
RequestPhysicalInventoryPollingMessage message = (RequestPhysicalInventoryPollingMessage) um
.unmarshal(is);
return message;
}
}
因为英语很烂,不知道用英语写的注释有没有写错。。。中文重新说明下:
RequestPhysicalInventoryPollingMessage message = (RequestPhysicalInventoryPollingMessage) um
.unmarshal(is);
这个方法:unmarshal() ,他不能接受String类型的参数(个人很不理解,为什么jaxb不去实现简单的把xml写在String中的POJO转化)。所以如果现在有一个String,内容是xml,就必须先调用这个String的getBytes()方法,然后用得到的byte[]去创建一个ByteArrayInputStream。然后把这个InputStream作为参数调用unmarshal().
执行代码结果如下:
Response status code: 200
Response body:
<?xml version="1.0" encoding="UTF-8"?>
<RequestPhysicalInventoryPollingMessage><interval>-1</interval><from>http://www.altova.com</from><to>http://www.altova.com</to><pollingurl>http://10.10.27.74:7001/ManagePhysicalInventoryConsumerProject/ManagePhysicalInventoryConsumerProxyService/String</pollingurl></RequestPhysicalInventoryPollingMessage>
interval:-1
from:http://www.altova.com
to:http://www.altova.com
pollinguri:http://10.10.27.74:7001/ManagePhysicalInventoryConsumerProject/ManagePhysicalInventoryConsumerProxyService/String