xml转json

第1章 快速入门

1.1 设计目的

xml格式和json格式作为前后端交互的主要方面,为了减轻后端处理xml格式的复杂问题,以及代码的优雅处理,开发设计了xml转json的方案。

1.2 系统设计介绍

系统提供内嵌和web访问方案。
内嵌的机制主要用于开发代码设计使用。
Web访问的机制,主要用于快速结构xml和json格式数据,便于用户快速分析数据结构使用。

1.3 内嵌项目集成方式

1.3.1pom引入

<dependency>
    <groupId>com.hecore</groupId>
    <artifactId>common-bdp</artifactId>
    <version>v-1.0</version>
</dependency>

1.3.2Controller

package com.hecore.cbs.controller;

import com.alibaba.fastjson.JSONObject;
import com.hecore.cbs.service.IXmlTransJsonService;
import com.hecore.rest.ReturnT;
import com.yuanian.ecs1.utils.ApiUtil;
import com.yuanian.ecs1.utils.model.ApiRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: hecore
 * @Date: 2021/7/8 13:53
 * @Description:
 */

@RestController
@RequestMapping("/xml")
public class XmlTransJsonController {

    @Autowired
    private IXmlTransJsonService xmlTransJsonService;

    @PostMapping("/trans")
    public Object index(@RequestBody String xml){
        return xmlTransJsonService.transXmlToJson(xml);
    }

}
1.3.3Service接口
package com.hecore.cbs.service;

import com.alibaba.fastjson.JSONObject;

/**
 * @Author: hecore
 * @Date: 2021/7/8 13:56
 * @Description:
 */

public interface IXmlTransJsonService {

    Object transXmlToJson(String xml);

}

1.3.4service接口实现类

package com.hecore.cbs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hecore.cbs.service.IXmlTransJsonService;
import com.hecore.rest.ReturnT;
import com.hecore.util.kit.XmlConvertKit;
import org.springframework.stereotype.Service;

/**
 * @Author: hecore
 * @Date: 2021/7/8 13:56
 * @Description:
 */

@Service
public class XmlTransJsonServiceImpl implements IXmlTransJsonService {

    @Override
    public Object transXmlToJson(String xml) {
        try {
            return XmlConvertKit.transJsonObject(xml);
        }catch (Exception e){
            return new ReturnT(ReturnT.FAIL_CODE,e.getMessage());
        }
    }
}

1.4 POSTMAN使用测试

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值