前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot实现xml传参和返回值

Spring Boot实现xml传参和返回值

作者头像
BUG弄潮儿
发布2020-06-15 16:37:46
2.2K0
发布2020-06-15 16:37:46
举报
文章被收录于专栏:JAVA乐园JAVA乐园

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

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

代码语言:javascript
复制
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>


 <groupId>com.jemter</groupId>
 <artifactId>com-lesson17</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>


 <name>com-lesson1</name>
 <url>http://maven.apache.org</url>


 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.0.4.RELEASE</version>
 </parent>


 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>


 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>


 <dependency>
 <groupId>com.fasterxml.jackson.dataformat</groupId>
 <artifactId>jackson-dataformat-xml</artifactId>
 </dependency>
 </dependencies>
</project>

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

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

教师实体类

代码语言:javascript
复制
package com.lesson17.model;


import java.util.List;


import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;


@JacksonXmlRootElement(localName = "MESSAGE")
public class Teacher {


 private Integer id;


 private String teacherName;


 private List<Student> studentList;


 @JacksonXmlProperty(isAttribute = true, localName = "TEACHER_ID")
 public Integer getId() {
 return id;
 }


 public void setId(Integer id) {
 this.id = id;
 }


 @JacksonXmlProperty(localName = "TEACHER_NAME")
 public String getTeacherName() {
 return teacherName;
 }


 public void setTeacherName(String teacherName) {
 this.teacherName = teacherName;
 }


 @JacksonXmlElementWrapper(localName = "STUDENT_LIST")
 @JacksonXmlProperty(localName = "STUDENT")
 public List<Student> getStudentList() {
 return studentList;
 }


 public void setStudentList(List<Student> studentList) {
 this.studentList = studentList;
 }


 @Override
 public String toString() {
 return "Teacher{" + "id=" + id + ", teacherName=" + teacherName + ", studentList=" + studentList + "}";
 }
}

学生实体类

代码语言:javascript
复制
package com.lesson17.model;


import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;


public class Student {
 private Integer id;


 private String stuName;


 private String sex;


 @JacksonXmlProperty(isAttribute = true, localName = "STUDENT_ID")
 public Integer getId() {
 return id;
 }


 public void setId(Integer id) {
 this.id = id;
 }


 @JacksonXmlProperty(localName = "STUDENT_NAME")
 public String getStuName() {
 return stuName;
 }


 public void setStuName(String stuName) {
 this.stuName = stuName;
 }


 @JacksonXmlProperty(localName = "STUDENT_SEX")
 public String getSex() {
 return sex;
 }


 public void setSex(String sex) {
 this.sex = sex;
 }


 @Override
 public String toString() {
 return "Student{" + "id=" + id + ", stuName=" + stuName + ", sex=" + sex + "}";
 }
}

3、编程控制类

代码语言:javascript
复制
package com.lesson17.controller;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import com.lesson17.model.Student;
import com.lesson17.model.Teacher;


@RestController
@RequestMapping("/teacher")
public class TeacherController {


 @RequestMapping(value = "/getInfo", method = RequestMethod.GET, produces = "application/xml")
 @ResponseBody
 public Teacher getInfo() {
 Student student1 = new Student();
        student1.setId(1);
        student1.setStuName("张三");
        student1.setSex("男");
 Student student2 = new Student();
        student2.setId(2);
        student2.setStuName("李四");
        student2.setSex("男");
 Teacher teacher = new Teacher();
        teacher.setId(11);
        teacher.setTeacherName("杨老师");
        teacher.setStudentList(Arrays.asList(student1, student2));
 return teacher;
 }


 @RequestMapping(value = "/postInfo", method = RequestMethod.POST, consumes = "application/xml")
 public Map<String, Object> postInfo(@RequestBody Teacher teacher) {
 System.out.println("postman传过来的xml信息转换成实体类如下:==========" + teacher.toString());
 Map<String, Object> results = new HashMap<String, Object>();
         results.put("code", "000000");
         results.put("msg", "success");
 return results; 
 }
}

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

4、编写SpringBoot启动类

代码语言:javascript
复制
package com.lesson17;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {


 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 }


}

5、application.yml配置如下

代码语言:javascript
复制
server:
  port: 8080
  servlet:
    context-path: /lesson17


spring:
  application:
    name: jmeter-lesson17

6、启动验证

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

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档