1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package io.github.pnoker.gateway.control;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import io.github.pnoker.gateway.JPBean.JPKprXuChangWaterSS;
- import io.github.pnoker.gateway.bizmgr.KprXuChangWaterBizFun;
- import io.github.pnoker.gateway.comtool.CommTool;
- import io.github.pnoker.gateway.comtool.HttpTool;
- import io.github.pnoker.gateway.config.GlobalData;
- import org.springframework.core.io.buffer.DataBuffer;
- import org.springframework.core.io.buffer.DataBufferUtils;
- import org.springframework.http.server.reactive.ServerHttpRequest;
- import org.springframework.util.CollectionUtils;
- import org.springframework.util.StringUtils;
- import org.springframework.web.bind.annotation.RequestHeader;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.reactive.function.client.WebClient;
- import reactor.core.publisher.Flux;
- import reactor.core.publisher.Mono;
- import retrofit2.http.POST;
- import io.github.pnoker.gateway.comtool.ResponseCode;
- import javax.servlet.http.HttpServletRequest;
- import java.nio.CharBuffer;
- import java.nio.charset.StandardCharsets;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.atomic.AtomicReference;
- /**
- * @ClassName TestController
- * @Description: TODO
- * @Author LX
- * @Date 2024/1/31
- * @Version V1.0
- **/
- @RestController
- @RequestMapping("/iot-cloud/gateway/xuchang/water")
- public class KprXuChangWaterController {
- WebClient webClient = WebClient.builder().codecs(configurer->configurer.defaultCodecs().maxInMemorySize(262144)).build();
- @RequestMapping(value = "/newlist",method = {RequestMethod.POST})
- public Mono<JSONObject> testMono(ServerHttpRequest serverHttpRequest){
- JSONObject oRes = new JSONObject();
- long llReqBefore = System.currentTimeMillis();
- oRes.set("rescode",ResponseCode.RESULT_BAD.toStrCode());
- oRes.set("msg",".error");
- oRes.set("resdata","");
- oRes.set("timestamp", System.currentTimeMillis());
- try {
- //TODO webflux获取body
- Flux<DataBuffer> body = serverHttpRequest.getBody();
- AtomicReference<String> bodyRef = new AtomicReference<>();
- body.subscribe(buffer -> {
- CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
- DataBufferUtils.release(buffer);
- bodyRef.set(charBuffer.toString());
- });
- String bodyStr = bodyRef.get();
- if(StringUtils.isEmpty(bodyStr)){
- oRes.set("rescode", ResponseCode.STATUS_ERROR_JSON_FORMAT.toStrCode());
- oRes.set("msg", ResponseCode.STATUS_ERROR_JSON_FORMAT.toStrMsg());
- return Mono.just(oRes);
- }
- com.alibaba.fastjson.JSONObject jObj = com.alibaba.fastjson.JSONObject.parseObject(bodyStr);
- JPKprXuChangWaterSS oJsonParam = jObj.toJavaObject(JPKprXuChangWaterSS.class);
- if (oJsonParam == null
- || !oJsonParam.checkValid()) {
- oRes.set("rescode", ResponseCode.STATUS_ERROR_JSON_FORMAT.toStrCode());
- oRes.set("msg", ResponseCode.STATUS_ERROR_JSON_FORMAT.toStrMsg());
- return Mono.just(oRes);
- }
- List<Map<String, Object>> resResultList = KprXuChangWaterBizFun.dataCenterTogether(oJsonParam);
- if (!CollectionUtils.isEmpty(resResultList)) {
- oRes.set("rescode", ResponseCode.RESULT_NORMAL.toStrCode());
- oRes.set("msg", ".success");
- oRes.set("resdata", com.alibaba.fastjson.JSONObject.toJSONString(resResultList, SerializerFeature.WriteMapNullValue));
- }
- return Mono.just(oRes);
- }catch(Exception ex){
- ex.printStackTrace();
- oRes.set("rescode", ResponseCode.RESULT_BAD.toStrCode());
- oRes.set("msg", ".error");
- return Mono.just(oRes);
- }
- }
- }
|