我是靠谱客的博主 包容白猫,最近开发中收集的这篇文章主要介绍springboot传 xml 和接受 xml 格式的字符串,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 引入依赖包

2. 实体类添加注解

3.测试

4. postMan 测试结果

1. 引入依赖包

 <!--请求体 是 application/xml  2entity-->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.9.8</version>
        </dependency>

2. 实体类添加注解

package com.example.demo.entity.xml2entity;

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;

/**
 * @program: springboot_01
 * @description:
 * @author: guoyiguang
 * @create: 2021-07-01 15:45
 **/
@JacksonXmlRootElement(localName ="USER_MESSAGE")
public class User {
    private Integer id;
    private String name;
    private String sex;
    private String phone;
    private List<UserOrder> orderList;


    @JacksonXmlProperty(localName = "USER_ID")
    public Integer getId() {
        return id;
    }

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

    @JacksonXmlProperty(localName = "USER_NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


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

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

    @JacksonXmlProperty(localName = "USER_PHONE")
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    @JacksonXmlProperty(localName = "USER_ORDERLIST")
    //集合的话必须添加这个注解
    @JacksonXmlElementWrapper(localName ="USER_ORDER_LIST")
    public List<UserOrder> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<UserOrder> orderList) {
        this.orderList = orderList;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", sex='" + sex + ''' +
                ", phone='" + phone + ''' +
                ", orderList=" + orderList +
                '}';
    }
}
package com.example.demo.entity.xml2entity;

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

/**
 * @program: springboot_01
 * @description: 用户订单
 * @author: guoyiguang  并添加xml和和bean转换的注解(注意,注解要写在get方法上)
 * @create: 2021-07-01 15:45
 **/
public class UserOrder {

    private Integer id;
    private String orderNum;
    private String orderName;

    @JacksonXmlProperty(localName = "ORDER_ID")
    public Integer getId() {
        return id;
    }

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


    @JacksonXmlProperty(localName = "ORDER_NUM")
    public String getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }


    @JacksonXmlProperty(localName = "ORDER_NAME")
    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    @Override
    public String toString() {
        return "UserOrder{" +
                "id=" + id +
                ", orderNum='" + orderNum + ''' +
                ", orderName='" + orderName + ''' +
                '}';
    }
}

3.测试

package com.example.demo.controller;
import com.example.demo.entity.xml2entity.User;
import com.example.demo.entity.xml2entity.UserOrder;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @program: springboot_01
 * @description:
 * @author: guoyiguang
 * @create: 2021-01-16 13:16
 **/
//只用  Controller 有可能报 404
@RestController
public class Xml2EntityController {



    @RequestMapping(value = "/xml2entity/getUser",method = RequestMethod.GET, produces = "application/xml")
    @ResponseBody
    public User getUser(){

        UserOrder order1 = new UserOrder();
        order1.setId(1);
        order1.setOrderNum("1111111111");
        order1.setOrderName("1111111111_充话费");

        UserOrder order2 = new UserOrder();
        order2.setId(2);
        order2.setOrderNum("22222222223");
        order2.setOrderName("2222222222_充话费");

        User user1 = new User();
        user1.setId(1);
        user1.setName("张三");
        user1.setSex("男");
        user1.setPhone("15011133056");
        user1.setOrderList(Arrays.asList(order1,order2));

        return user1;
    }

    @PostMapping(value = "/xml2entity/submitA", consumes = "application/xml")
    public String submitA(@RequestBody User user){
        System.out.println("postman传过来的xml信息转换成实体类如下:==========");
        System.out.println(user.toString());
        return user.toString();
    }

    @RequestMapping(value = "/xml2entity/submitB")
    public String submitB(){
        return "success";
    }








}

 

4. postMan 测试结果

 //集合的话必须添加这个注解
    @JacksonXmlElementWrapper(localName ="USER_ORDERLIST")
    //集合内部的元素
    @JacksonXmlProperty(localName = "URL")
    public List<UserOrder> getOrderList() {
        return orderList;
    }

 

@JacksonXmlRootElement
public class UserOrder {

    private Integer id;
    private String orderNum;


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

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


    @JacksonXmlText()
    public String getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }

}

最后

以上就是包容白猫为你收集整理的springboot传 xml 和接受 xml 格式的字符串的全部内容,希望文章能够帮你解决springboot传 xml 和接受 xml 格式的字符串所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部