Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 如何将XML数据发送到我的服务器,它是当前的端点_Java_Spring_Spring Boot - Fatal编程技术网

Java 如何将XML数据发送到我的服务器,它是当前的端点

Java 如何将XML数据发送到我的服务器,它是当前的端点,java,spring,spring-boot,Java,Spring,Spring Boot,我正在尝试读取json并将其以XML的形式发送回服务器。我能够成功地读取json,但我需要从我在端点中读取的内容发送一个xml URL url = new URL("https://xxxx.xxx/xxxx/post"); HttpURLConnection http = (HttpURLConnection)url.openConnection(); http.setRequestMethod("POST"); http.setDoOu

我正在尝试读取json并将其以XML的形式发送回服务器。我能够成功地读取json,但我需要从我在端点中读取的内容发送一个xml

  URL url = new URL("https://xxxx.xxx/xxxx/post");
  HttpURLConnection http = (HttpURLConnection)url.openConnection();
  http.setRequestMethod("POST");
  http.setDoOutput(true);
  http.setRequestProperty("Content-Type", "application/xml");
  http.setRequestProperty("Accept", "application/xml");

  String data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Request>\n    <Login>login</Login>\n    
  <Password>password</Password>\n</Request>";

  byte[] out = data.getBytes(StandardCharsets.UTF_8);

  OutputStream stream = http.getOutputStream();
  stream.write(out);

  System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
  http.disconnect();
URL=新URL(“https://xxxx.xxx/xxxx/post");
HttpURLConnection http=(HttpURLConnection)url.openConnection();
http.setRequestMethod(“POST”);
http.setDoOutput(true);
setRequestProperty(“内容类型”、“应用程序/xml”);
setRequestProperty(“接受”、“应用程序/xml”);
String data=“\n\n登录\n
密码\n“;
byte[]out=data.getBytes(StandardCharsets.UTF_8);
OutputStream=http.getOutputStream();
流。写(出);
System.out.println(http.getResponseCode()+“”+http.getResponseMessage());
http.disconnect();
当前im硬编码字符串数据,但我想发送rest端点中的数据http://localhost:8080/login 如果我点击这个端点,我会得到一个XML

<ArrayList>
  <item>
    <Login>1000<Login>
    <Password>Pwd<Password>
  </item>
</ArrayList>

1000
Pwd

如何读取该端点并将其用作字符串数据

我不经常回答,但我想我遇到过这样的情况。 如果我理解正确,您希望将JSON字符串转换为XML

我想您可以使用封送处理程序将JOSN转换为POJO对象(或者转换为JSONObject),然后将其封送处理为XML。对于一个简单的解决方案,我建议使用Jackson(另外,如果您使用的是像Spring Boot Jackson附带的东西)
马文

我们可以创建一个Java类,如:

class MappingObject{
    public Integer id;
    public String name;
//Getters and setters 
...
}
现在我们可以使用封送拆收器将其转换为POJO,然后再转换为XML

ObjectMapper objectMapper = new ObjectMapper();
List<MappingObject> parsedDataList= objectMapper.readValue(result, new TypeReference<List<MappingObject>>(){});
XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(reparsedDataList);
System.out.println("This is the XML version of the Output : "+xml);
ObjectMapper ObjectMapper=new ObjectMapper();
List parsedDataList=objectMapper.readValue(结果,新类型引用(){});
XmlMapper mapper=新的XmlMapper();
字符串xml=mapper.writeValueAsString(reparsedDataList);
System.out.println(“这是输出的XML版本:“+XML”);
我认为这个问题与你的问题很接近:

class MappingObject{
    public Integer id;
    public String name;
//Getters and setters 
...
}
ObjectMapper objectMapper = new ObjectMapper();
List<MappingObject> parsedDataList= objectMapper.readValue(result, new TypeReference<List<MappingObject>>(){});
XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(reparsedDataList);
System.out.println("This is the XML version of the Output : "+xml);