ludc
2024-02-29 8ef9e366be48dc5e8e52617ea8ed48b37a0e1f74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
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("/pushIntegration")
public class PushIntegrationController {
 
    /**
     * 推送接口请求类型:post
     * Content-Type类型为x-www-form-urlencoded,或param时接口定义方式  1
     * @param dataType
     * @param dataString
     * @return
     * @throws IOException
     */
    @PostMapping("/testByForm1")
    public R test(@RequestParam String dataType, @RequestParam String dataString) throws IOException {
        // 相关逻辑处理
        System.out.println(dataType);
        System.out.println(dataString);
        return R.status(true);
    }
 
    /**
     * 推送接口请求类型:post
     * Content-Type类型为x-www-form-urlencoded,或param时接口定义方式  2
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/testByForm2")
    public R 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 R.data(jsonArray);
    }
 
    /**
     * 推送接口请求类型:post
     * Content-Type类型为application/json时接口定义方式
     * @param requestData
     * @return
     * @throws IOException
     */
    @PostMapping("/testByAppjson")
    public R test1(@RequestBody RequestData requestData) throws IOException {
        // 拿到这个参数之后取该集合中的第一个元素,就是要json转成对象的参数
        // 如有具体转换要求,则根据编码器中的接口提供的配置而定,通常为dataType字段,如xml,json两种情况
        List<Object> dataString = requestData.getDataString();
        // JSONObject.parseObject(dataString,/*根据编码系统提供的对象属性而定义的对象*/);
        List<String> dataType = requestData.getDataType();
        return R.data(dataString);
    }
 
}