package com.vci.ubcs.example.controller;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.vci.ubcs.example.entity.RequestData;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.ServletInputStream;
|
import javax.servlet.ServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.IOException;
|
import java.nio.charset.StandardCharsets;
|
import java.util.List;
|
|
/**
|
* 编码资源管理系统对其他系统的统一推送,被调方定义方式
|
* UBCS编码资源管理系统对
|
* 其他集成的系统进行推送的,
|
* 接受推送的api定义示例
|
* @author ludc
|
* @date 2024/2/27 20:31
|
*/
|
@RestController
|
@RequestMapping("/pushIntegration")
|
public class PushIntegrationController {
|
|
/**
|
* 推送接口请求类型:post
|
* Content-Type类型为x-www-form-urlencoded,或param时接口定义方式 1
|
* @param dataType
|
* @param dataString
|
* @return
|
* @throws IOException
|
*/
|
@PostMapping("/testByForm1")
|
public String test(@RequestParam String dataType, @RequestParam String dataString) throws IOException {
|
// 相关逻辑处理
|
System.out.println(dataType);
|
System.out.println(dataString);
|
return "成功";
|
}
|
|
/**
|
* 推送接口请求类型:post
|
* Content-Type类型为x-www-form-urlencoded,或param时接口定义方式 2
|
* @param request
|
* @return
|
* @throws IOException
|
*/
|
@PostMapping("/testByForm2")
|
public String test1(ServletRequest request) throws IOException {
|
HttpServletRequest request1 = (HttpServletRequest) request;
|
String dataString = request1.getParameter("dataString");
|
ServletInputStream inputStream = request1.getInputStream();
|
// 读取数据
|
byte[] buffer = new byte[4096];
|
int len = inputStream.read(buffer);
|
while (len != -1) {
|
// do something with buffer
|
len = inputStream.read(buffer);
|
}
|
String body = new String(buffer, StandardCharsets.UTF_8);
|
JSONObject jsonObject = JSONObject.parseObject(body);
|
JSONArray jsonArray = jsonObject.getJSONArray("dataString");
|
|
return jsonArray.toJSONString();
|
}
|
|
/**
|
* 推送接口请求类型:post
|
* Content-Type类型为application/json时接口定义方式
|
* @param requestData
|
* @return
|
* @throws IOException
|
*/
|
@PostMapping("/testByAppjson")
|
public String test1(@RequestBody RequestData requestData) throws IOException {
|
// 拿到这个参数之后取该集合中的第一个元素,就是要json转成对象的参数
|
// 如有具体转换要求,则根据编码器中的接口提供的配置而定,通常为dataType字段,如xml,json两种情况
|
List<Object> dataString = requestData.getDataString();
|
// JSONObject.parseObject(dataString,/*根据编码系统提供的对象属性而定义的对象*/);
|
List<String> dataType = requestData.getDataType();
|
return JSONObject.toJSONString(dataString);
|
}
|
|
}
|