0


后端接收与发送XML数据

后端接收与发送XML数据

1 接收发送来的XML数据

1.1 普通XML

①postman发送请求,格式为XML
修改Content-Type格式为:text/xml或者application/xml
在这里插入图片描述

在这里插入图片描述
②后端接收

  • 导入依赖
<!--XML解析--><dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency>
  • 编写接收实体类
@Data@AllArgsConstructor@NoArgsConstructor@JacksonXmlRootElement(localName ="User")publicclassUser{@JacksonXmlProperty(localName ="name")privateString name;@JacksonXmlProperty(localName ="age")privateInteger age;}
  • controller接收数据
@RestControllerpublicclassTestXMLController{@PostMapping("/testXML")publicStringtestXMLController(@RequestBodyUser user){System.out.println(user);return"testXML success";}}
  • 结果:
User(name=curry, age=29)

1.2 复杂XML(嵌套)

①postman发送XML数据

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Root><head><provider>江南造汽场</provider><address>江南皮革厂旁边</address><date>2022-10-10 11:21:43</date></head><info><car><name>兰博基尼</name><type>敞篷跑车</type><count>10</count></car><car><name>法拉利</name><type>超跑</type><count>5</count></car></info></Root>

在这里插入图片描述

header选择:

Content-Type为application/xml

在这里插入图片描述
②后端解析XML数据

  • 解析根标签【Root】
@Data@JacksonXmlRootElement(localName ="Root")@AllArgsConstructor@NoArgsConstructorpublicclassRootXml{@JacksonXmlElementWrapper(localName ="head")privateHeadInfo headInfo;@JacksonXmlElementWrapper(localName ="info")@JacksonXmlProperty(localName ="car")privateList<Car> carList;}
  • 解析head标签
@Data@AllArgsConstructor@NoArgsConstructor@JacksonXmlRootElement(localName ="Root")publicclassHeadInfo{@JacksonXmlProperty(localName ="provider")privateString facName;@JacksonXmlProperty(localName ="address")privateString address;@JacksonXmlProperty(localName ="date")//也可以使用@DateTimeFormat注解转换,但无法精确到小时//此处也可以改成String,然后使用DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")转换@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss", timezone ="GMT+8")privateDate date;}
  • 解析info标签
@Data@AllArgsConstructor@NoArgsConstructor@JacksonXmlRootElement(localName ="Root")publicclassCarInfos{@JacksonXmlElementWrapper(localName ="info")@JacksonXmlProperty(localName ="car")privateList<Car> carList;}
  • 解析car标签
//车辆信息@AllArgsConstructor@NoArgsConstructor@DatapublicclassCar{@JacksonXmlProperty(localName ="name")privateString carName;@JacksonXmlProperty(localName ="type")privateString carType;@JacksonXmlProperty(localName ="count")privateInteger count;}

③编写controller接收XML并获取数据

produces = MediaType.APPLICATION_XML_VALUE, consumes = MediaType.APPLICATION_XML_VALUE

@PostMapping(value ="/parseXML", produces =MediaType.APPLICATION_XML_VALUE, consumes =MediaType.APPLICATION_XML_VALUE)publicStringparseXML(@RequestBodyRootXml rootXml){HeadInfo headInfo = rootXml.getHeadInfo();System.out.println("headInfo:"+ headInfo);List<Car> carList = rootXml.getCarList();for(Car car : carList){System.out.println(car);}return"parseXML success";}

④控制台信息

headInfo:HeadInfo(facName=江南造汽场, address=江南皮革厂旁边, date=MonOct1011:21:43CST2022)Car(carName=兰博基尼, carType=敞篷跑车, count=10)Car(carName=法拉利, carType=超跑, count=5)

2 后端发送XML数据

①导入依赖

<!--XML解析--><dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency>

②编写实体类

@Data@AllArgsConstructor@NoArgsConstructor@JacksonXmlRootElement(localName ="User")publicclassUser{@JacksonXmlProperty(localName ="name")privateString name;@JacksonXmlProperty(localName ="age")privateInteger age;}

③编写controller

注解上添加:produces = MediaType.APPLICATION_XML_VALUE

@RestControllerpublicclassTestXMLController{@PostMapping(value ="/sendXML", produces =MediaType.APPLICATION_XML_VALUE)publicUsersendXML(){User user =newUser();
        user.setName("徐杰");
        user.setAge(18);return user;}}

本文主要使用Jackson来发送解析XML数据,详细操作
https://blog.csdn.net/javas_/article/details/79142358
GitHub地址:https://github.com/FasterXML/jackson-dataformat-xml/wiki/Jackson-XML-annotations
④postman发起请求,查看返回结果
在这里插入图片描述

标签: xml java postman

本文转载自: https://blog.csdn.net/weixin_45565886/article/details/127258837
版权归原作者 NPE~ 所有, 如有侵权,请联系我们删除。

“后端接收与发送XML数据”的评论:

还没有评论