spring boot中使用xml请求

spring boot 中使用xml请求

  1. 先在pom文件中引入依赖包

    <dependency>   <groupId>com.fasterxml.jackson.dataformat</groupId>   <artifactId>jackson-dataformat-xml</artifactId>   <version>2.9.0</version></dependency>
  2. 编写请求实体
    package com.scai.perfirmance.request;
    
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;import com.scai.perfirmance.bean.User;
    
    import java.util.List;
    
    @JacksonXmlRootElement(localName = "message")public class UserRequest {
    
       private List<User> userList;
    
       @JacksonXmlElementWrapper(localName = "userlist")   @JacksonXmlProperty(localName = "user")   public List<User> getUserList() {      return userList;   }
    
       public void setUserList(List<User> userList) {      this.userList = userList;   }}
    package com.scai.perfirmance.bean;
    
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
    
    public class User {   @JacksonXmlProperty(localName = "idcard")   private String idCard;   @JacksonXmlProperty(localName = "username")   private String name;   @JacksonXmlProperty(localName = "sex")   private String sex;   @JacksonXmlProperty(localName = "age")   private int age;
    
       public String getIdCard() {      return idCard;   }
    
       public void setIdCard(String idCard) {      this.idCard = idCard;   }
    
       public String getName() {      return name;   }
    
       public void setName(String name) {      this.name = name;   }
    
       public String getSex() {      return sex;   }
    
       public void setSex(String sex) {      this.sex = sex;   }
    
       public int getAge() {      return age;   }
    
       public void setAge(int age) {      this.age = age;   }
    
    }
  3. 返回实体
    package com.scai.perfirmance.response;
    
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
    
    import java.util.List;
    
    @JacksonXmlRootElement(localName = "message")public class UserResponse {
    
       private List<UserRes> userResList;
    
       @JacksonXmlElementWrapper(localName = "userreslist")// @JacksonXmlElementWrapper(useWrapping = false)  // 禁用该属性参与xml转换   @JacksonXmlProperty(localName = "userres")   public List<UserRes> getUserResList() {      return userResList;   }
    
       public void setUserResList(List<UserRes> userResList) {      this.userResList = userResList;   }
    
    }
    package com.scai.perfirmance.response;
    
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;import com.scai.perfirmance.bean.User;
    
    public class UserRes  extends User {
    
        private String salary;
    
       @JacksonXmlProperty(localName = "salary")   public String getSalary() {      return salary;   }
    
       public void setSalary(String salary) {      this.salary = salary;   }}
  4. 编写Contoller 
    package com.scai.perfirmance.controller;
    
    import com.scai.perfirmance.request.UserRequest;import com.scai.perfirmance.response.UserRes;import com.scai.perfirmance.response.UserResponse;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.*;
    
    import java.util.ArrayList;import java.util.List;
    
    @Api(description="xml接口测试")@RestController@RequestMapping("/xmlTest")
    
    public class UserXmlController {
    
       @ApiOperation(value = "xml接口测试",notes = "xml接口测试")   @PostMapping(value = "/user",consumes = MediaType.APPLICATION_XML_VALUE,produces = MediaType.APPLICATION_XML_VALUE)
    
       public UserResponse testXml(@RequestBody UserRequest userRequest){
    
          UserResponse res =new UserResponse();      List<UserRes> list = new ArrayList<UserRes>();
    
          UserRes userRes = new UserRes();      userRes.setIdCard("11111111111111");      userRes.setName("sky_fxh");//    userRes.setAge(30);      userRes.setSalary("5000");      list.add(userRes);      res.setUserResList(list);      return  res;   }
    
    }
  5. 扩展说明

    @JacksonXmlElementWrapper:可用于指定List等集合类,外围标签名;
    @JacksonXmlProperty:指定包装标签名,或者指定标签内部属性名;
    @JacksonXmlRootElement:指定生成xml根标签的名字;
    @JacksonXmlText:指定当前这个值,没有xml标签包裹。

    实体bean中也可以使用  @XmlRootElement 和@XmlElement这个标签,分别对应于@JacksonXmlRootElement 和@JacksonXmlProperty

  6. 测试结果 请求

    <?xml version="1.0" encoding="UTF-8"?>
    <message>
    <userlist>
    <user>
    <age>0</age>
    <idcard>111111111111111</idcard>
    <username>sky_fxh</username>
    <sex>man</sex>
    </user>
    </userlist>
    </message>
  7. 测试结果,返回

    <message>
    <userreslist>
    <userres>
    <idcard>11111111111111</idcard>
    <username>sky_fxh</username>
    <sex/>
    <age>0</age>
    <salary>5000</salary>
    </userres>
    </userreslist>
    </message>


原文地址:https://www.cnblogs.com/scai-fxh/p/10598535.html

时间: 2024-08-01 17:47:56

spring boot中使用xml请求的相关文章

Spring Boot中使用AOP统一处理Web请求日志

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通过对既有程序定义一个切入点,然后在其前后切入不同的执行内容,比如常见的有:打开数据库连接/关闭数据库连接.打开事务/关闭事务.记录日志等.基于AOP不会破坏原来程序逻辑,因此它可以很好的对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率.

3.Spring Boot中使用Swagger2构建强大的RESTful API文档

原文:http://www.jianshu.com/p/8033ef83a8ed 由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这些终端会共用很多底层业务逻辑,因此我们会抽象出这样一层来同时服务于多个移动端或者Web前端. 这样一来,我们的RESTful API就有可能要面对多个开发人员或多个开发团队:IOS开发.Android开发或是Web开发

Spring Boot中的注解

文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了解决EJB等大型企业框架对应用程序的侵入性,因此大量依靠配置文件来“非侵入式”得给POJO增加功能,然而,从Spring 3.x开始,Spring被外界最为诟病的一点就是配置繁多,号称“配置地狱”,各种xml文件,出了问题非常难排查.从Spring 4.x开始,Spring.io提供了三种方式编织B

Spring Boot中使用Swagger2生成RESTful API文档(转)

效果如下图所示: 添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <versi

Spring Boot中使用Swagger2构建强大的RESTful API文档

由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这些终端会共用很多底层业务逻辑,因此我们会抽象出这样一层来同时服务于多个移动端或者Web前端. 这样一来,我们的RESTful API就有可能要面对多个开发人员或多个开发团队:IOS开发.Android开发或是Web开发等.为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTf

Spring Boot中使用Swagger2构建API文档

程序员都很希望别人能写技术文档,自己却很不愿意写文档.因为接口数量繁多,并且充满业务细节,写文档需要花大量的时间去处理格式排版,代码修改后还需要同步修改文档,经常因为项目时间紧等原因导致文档滞后于代码,接口调用方的抱怨声不绝于耳.而程序员是最擅长"偷懒"的职业了,自然会有多种多样的自动生成文档的插件.今天要介绍的就是Swagger. 接下来我们在Spring Boot中使用Swagger2构建API文档 Swagger是一个简单但功能强大的API表达工具.它具有地球上最大的API工具生

spring-boot实战【07】【转】:Spring Boot中Web应用的统一异常处理

我们在做Web应用的时候,请求处理过程中发生错误是非常常见的情况.Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 选择一个之前实现过的Web应用(Chapter3-1-2)为基础,启动该应用,访问一个不存在的URL,或是修改处理内容,直接抛出异常,如: 1 2 3 4 @RequestMapping("/hello") public String hello() throws Exce

spring-boot实战【12】:Spring Boot中使用JavaMailSender发送邮件

相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件. 快速入门 在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖: 1 2 3 4 <dependency> <groupId>org.springframew

Spring Boot中Web应用的统一异常处理

我们在做Web应用的时候,请求处理过程中发生错误是非常常见的情况.Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 选择一个之前实现过的Web应用(Chapter3-1-2)为基础,启动该应用,访问一个不存在的URL,或是修改处理内容,直接抛出异常,如: 1 2 3 4 @RequestMapping("/hello") public String hello() throws Exce