第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());
}
}
}