使用POSTMAN在POST请求中将XML发送到URL,并希望将XML发送到java变量

时间:2019-10-21 09:50:30

标签: java xml rest api

我与邮递员一起向Java Rest Api发送XML Post请求,我想在Java变量或对象中捕获XML

我尝试过:

@Stateless
@Path("/cererepost")
public class HelloWorldResource {
Resp x = new Resp();
@EJB
private NameStorageBean nameStorage;
/**
 * Retrieves representation of an instance of helloworld.HelloWorldResource
 * @return an instance of java.lang.String
 */
@POST
@Produces("application/xml")
@Consumes(MediaType.APPLICATION_XML)
public Response postMsg() throws ParserConfigurationException,    MalformedURLException, IOException, SAXException  {
  /*
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = (Document) db.parse(new URL("http://localhost:8080/REST4  /resources/cererepost").openStream());
 */
    URL obj = new URL("http://localhost:8080/REST4/resources/cererepost");
URLConnection conn = obj.openConnection();

//get all headers
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    System.out.println("Key : " + entry.getKey() + 
             " ,Value : " + entry.getValue());
}

//get header by 'key'
String server = conn.getHeaderField("Server");

    Response response =    Response.status(200).type(MediaType.TEXT_XML).entity(server).build();
    return response;

}

}

这是完整的代码。

我希望得到并回答如何修改的部分是

 URL obj = new URL("http://localhost:8080/REST4/resources/cererepost");
URLConnection conn = obj.openConnection();

//get all headers
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    System.out.println("Key : " + entry.getKey() + 
             " ,Value : " + entry.getValue());
}

//get header by 'key'
String server = conn.getHeaderField("Server");

    Response response = Response.status(200).type(MediaType.TEXT_XML).entity(server).build();
    return response;

在Postman发出Post请求后,它会立即返回,并返回服务器名称。我想返回已发送的xml,以便将变量保存在数据库中。

预先感谢

0 个答案:

没有答案