首页 > 其他分享 >Spring Boot实现xml传参和返回值

Spring Boot实现xml传参和返回值

时间:2023-06-02 15:01:23浏览次数:32  
标签:传参 xml String Spring id import com public


阅读文本大概需要3分钟。

       虽然json作为数据传输的格式大型其道,但是使用xml格式传输的系统还是在一些存量的系统中存在。另外WebService本身就是使用xml格式进行数据传输。今天用个小例子看看Spring Boot如何实现xml传参和返回值。

1、新建maven项目,添加依赖


1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2.  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3.  <modelVersion>4.0.0</modelVersion>
4. 
5.  <groupId>com.jemter</groupId>
6.  <artifactId>com-lesson17</artifactId>
7.  <version>0.0.1-SNAPSHOT</version>
8.  <packaging>jar</packaging>
9. 
10.  <name>com-lesson1</name>
11.  <url>http://maven.apache.org</url>
12. 
13.  <parent>
14.  <groupId>org.springframework.boot</groupId>
15.  <artifactId>spring-boot-starter-parent</artifactId>
16.  <version>2.0.4.RELEASE</version>
17.  </parent>
18. 
19.  <properties>
20.  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21.  </properties>
22. 
23.  <dependencies>
24.  <dependency>
25.  <groupId>org.springframework.boot</groupId>
26.  <artifactId>spring-boot-starter-web</artifactId>
27.  </dependency>
28. 
29.  <dependency>
30.  <groupId>com.fasterxml.jackson.dataformat</groupId>
31.  <artifactId>jackson-dataformat-xml</artifactId>
32.  </dependency>
33.  </dependencies>
34. </project>

jackson-dataformat-xml是xml和bean转换依赖的包

2、新建实体类,并添加xml和和bean转换的注解(注解要写在get方法上)

教师实体类


1. package com.lesson17.model;
2. 
3. import java.util.List;
4. 
5. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
6. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
7. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
8. 
9. @JacksonXmlRootElement(localName = "MESSAGE")
10. public class Teacher {
11. 
12.  private Integer id;
13. 
14.  private String teacherName;
15. 
16.  private List<Student> studentList;
17. 
18.  @JacksonXmlProperty(isAttribute = true, localName = "TEACHER_ID")
19.  public Integer getId() {
20.  return id;
21.  }
22. 
23.  public void setId(Integer id) {
24.  this.id = id;
25.  }
26. 
27.  @JacksonXmlProperty(localName = "TEACHER_NAME")
28.  public String getTeacherName() {
29.  return teacherName;
30.  }
31. 
32.  public void setTeacherName(String teacherName) {
33.  this.teacherName = teacherName;
34.  }
35. 
36.  @JacksonXmlElementWrapper(localName = "STUDENT_LIST")
37.  @JacksonXmlProperty(localName = "STUDENT")
38.  public List<Student> getStudentList() {
39.  return studentList;
40.  }
41. 
42.  public void setStudentList(List<Student> studentList) {
43.  this.studentList = studentList;
44.  }
45. 
46.  @Override
47.  public String toString() {
48.  return "Teacher{" + "id=" + id + ", teacherName=" + teacherName + ", studentList=" + studentList + "}";
49.  }
50. }

学生实体类


1. package com.lesson17.model;
2. 
3. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
4. 
5. public class Student {
6.  private Integer id;
7. 
8.  private String stuName;
9. 
10.  private String sex;
11. 
12.  @JacksonXmlProperty(isAttribute = true, localName = "STUDENT_ID")
13.  public Integer getId() {
14.  return id;
15.  }
16. 
17.  public void setId(Integer id) {
18.  this.id = id;
19.  }
20. 
21.  @JacksonXmlProperty(localName = "STUDENT_NAME")
22.  public String getStuName() {
23.  return stuName;
24.  }
25. 
26.  public void setStuName(String stuName) {
27.  this.stuName = stuName;
28.  }
29. 
30.  @JacksonXmlProperty(localName = "STUDENT_SEX")
31.  public String getSex() {
32.  return sex;
33.  }
34. 
35.  public void setSex(String sex) {
36.  this.sex = sex;
37.  }
38. 
39.  @Override
40.  public String toString() {
41.  return "Student{" + "id=" + id + ", stuName=" + stuName + ", sex=" + sex + "}";
42.  }
43. }

3、编程控制类


1. package com.lesson17.controller;
2. 
3. import java.util.Arrays;
4. import java.util.HashMap;
5. import java.util.Map;
6. 
7. import org.springframework.web.bind.annotation.RequestBody;
8. import org.springframework.web.bind.annotation.RequestMapping;
9. import org.springframework.web.bind.annotation.RequestMethod;
10. import org.springframework.web.bind.annotation.ResponseBody;
11. import org.springframework.web.bind.annotation.RestController;
12. 
13. import com.lesson17.model.Student;
14. import com.lesson17.model.Teacher;
15. 
16. @RestController
17. @RequestMapping("/teacher")
18. public class TeacherController {
19. 
20.  @RequestMapping(value = "/getInfo", method = RequestMethod.GET, produces = "application/xml")
21.  @ResponseBody
22.  public Teacher getInfo() {
23.  Student student1 = new Student();
24.  student1.setId(1);
25.  student1.setStuName("张三");
26.  student1.setSex("男");
27.  Student student2 = new Student();
28.  student2.setId(2);
29.  student2.setStuName("李四");
30.  student2.setSex("男");
31.  Teacher teacher = new Teacher();
32.  teacher.setId(11);
33.  teacher.setTeacherName("杨老师");
34.  teacher.setStudentList(Arrays.asList(student1, student2));
35.  return teacher;
36.  }
37. 
38.  @RequestMapping(value = "/postInfo", method = RequestMethod.POST, consumes = "application/xml")
39.  public Map<String, Object> postInfo(@RequestBody Teacher teacher) {
40.  System.out.println("postman传过来的xml信息转换成实体类如下:==========" + teacher.toString());
41.  Map<String, Object> results = new HashMap<String, Object>();
42.  results.put("code", "000000");
43.  results.put("msg", "success");
44.  return results; 
45.  }
46. }

注:关键步骤是RequestMapping注解的produces和consumes这两个属性,如果参数是xml,则需要把consumes配置成application/xml;如果是返回值是xml,则需要把把produces配置成application/xml。

4、编写SpringBoot启动类


1. package com.lesson17;
2. 
3. import org.springframework.boot.SpringApplication;
4. import org.springframework.boot.autoconfigure.SpringBootApplication;
5. 
6. @SpringBootApplication
7. public class Application {
8. 
9.  public static void main(String[] args) {
10.  SpringApplication.run(Application.class, args);
11.  }
12. 
13. }

5、application.yml配置如下


1. server:
2.  port: 8080
3.  servlet:
4.  context-path: /lesson17
5. 
6. spring:
7.  application:
8.  name: jmeter-lesson17

6、启动验证

请求http://127.0.0.1:8080/lesson17/teacher/getInfo接口

Spring Boot实现xml传参和返回值_maven

请求http://127.0.0.1:8080/lesson17/teacher/postInfo接口

Spring Boot实现xml传参和返回值_spring boot_02

关注我

每天进步一点点

Spring Boot实现xml传参和返回值_payment_03


标签:传参,xml,String,Spring,id,import,com,public
From: https://blog.51cto.com/u_13538361/6402766

相关文章

  • spring cloud gateway 自定义GatewayFilterFactory
    官网地址:https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#writing-custom-route-predicate-factories参考地址:https://blog.csdn.net/myli92/article/details/127328893importcom.ruoyi.common.core.utils.StringUtils;importorg.springfr......
  • springboot gradle dockerfle
    本地打包FROMopenjdk:8-jdk-alpineRUNset-eux&&sed-i's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g'/etc/apk/repositoriesRUNmkdir/appCOPYbuild/libs/dataExChangePlatform-0.0.1-SNAPSHOT.jar/app/dataExChangePlatform-0.0.1-SNAPSHOT.......
  • SpringBoot Vue3 Element Plus 打造分布式存储系统
    SpringBoot+Vue3+ElementPlus打造分布式存储系统download:3w51xuebccom配置IDEA热部署-devtools开发过程中频繁修改代码,每次都需要重新编译,部署,重启服务器,这无疑极大浪费了我们的时间。解决这个问题的方法就是使用热部署技术。本篇文章将介绍如何在IDEA中使用devtools实现热部署......
  • 蛋糕网上销售系统|基于Springboot+Vue开发实现网上蛋糕系统
    作者主页:编程指南针作者简介:Java领域优质创作者、多年架构师设计经验、腾讯课堂常驻讲师主要内容:Java项目、毕业设计、简历模板、学习资料、面试题库、技术互助文末获取源码 项目编号:BS-SC-051前言:伴随着互联网的“低门槛”和人们金钱消费的数字化转变,网络购物奕然已经成为了时代......
  • Android strings.xml按照key修改
    strings.xml匹配替换将两个Android项目中的多语言字符串文件(strings.xml)进行比较,如果其中一个项目中包含另一个项目没有的字符,则合并到单一的输出文件,并以key在原始XML文件中更新value值。如果key匹配不准确则忽略它。具体来说:引入re,xml.etree.ElementTree和argpar......
  • Spring Boot如何自定义监控指标
    1.创建项目pom.xml引入相关依赖<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd"......
  • maven创建web项目(springboot)
    创建一个Java项目和创建一个Web项目在IntelliJIDEA中有一些区别。创建Java项目:选择"Java"选项,并选择适当的JDK版本。创建的是一个普通的Java项目,用于编写Java类、库和应用程序。没有默认的Web相关依赖和配置。创建Web项目:选择"JavaEnterprise"选......
  • vue-router 4.1.4版本以上,打开新页面并隐式传参
    情景框架:Vue3+Vite4+VueRouter4.1.6需求:现需要实现点击打开新页面,并把一些数据传过去限制:数据量较大,有一定私密性,不适合用query传递;需要从session中获取token判断访问权限,因此需要用router.resolve携带session打开新页面使用params传参在4.1.4以上版本被抛弃了-->vu......
  • Jasypt加密SpringBoot配置文件和自动加密数据库敏感信息
    Jasypt是开源的加密和解密的组件。和Spring提供了很好的集成。一、加密SpringBoot配置文件 新建SpringBoot项目,添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>......
  • springboot 上传文件
    1、html代码:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><formaction="/upload"method="post"enctype=&qu......