Browse Source

feat: 1.修复中文无法接受的问题

zhangwl 1 month ago
parent
commit
78f54a1033

+ 13 - 0
pom.xml

@@ -30,6 +30,19 @@
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
+            <exclusions>
+                <!-- 排除内嵌 Tomcat,改用 Undertow(允许 URL 中未转义的中文/方括号) -->
+                <exclusion>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <!-- Undertow 容器(替代 Tomcat) -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-undertow</artifactId>
         </dependency>
 
         <!-- Kafka 依赖(关键) -->

+ 27 - 0
src/main/java/zs/payment/config/UndertowConfig.java

@@ -0,0 +1,27 @@
+package zs.payment.config;
+
+import io.undertow.UndertowOptions;
+import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
+import org.springframework.boot.web.server.WebServerFactoryCustomizer;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * Undertow 容器定制:允许 URL 中出现未转义字符(原始中文字节、方括号等),
+ * 复刻旧服务 Tomcat 8 的宽松行为,使 PHP 客户端未做 percent-encoding 的
+ * keyWord=整套、pts=[1200003] 等请求能被正常接收,并按 UTF-8 解码。
+ */
+@Configuration
+public class UndertowConfig {
+
+    @Bean
+    public WebServerFactoryCustomizer<UndertowServletWebServerFactory> undertowCustomizer() {
+        return factory -> factory.addBuilderCustomizers(builder -> {
+            // 允许 URL 中出现未转义的非法字符(含原始非 ASCII 字节与 [ ])
+            builder.setServerOption(UndertowOptions.ALLOW_UNESCAPED_CHARACTERS_IN_URL, true);
+            // 开启 URL 解码并指定字符集,确保原始 UTF-8 字节解码为中文
+            builder.setServerOption(UndertowOptions.DECODE_URL, true);
+            builder.setServerOption(UndertowOptions.URL_CHARSET, "UTF-8");
+        });
+    }
+}

+ 13 - 10
src/main/java/zs/payment/controller/YunMallGoodsController.java

@@ -3,6 +3,7 @@ package zs.payment.controller;
 
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -14,6 +15,8 @@ import zs.payment.req.yunmall.goods.GoodsSaveReq;
 import zs.payment.service.yunmall.goods.GoodsService;
 
 import javax.validation.Valid;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
 import java.util.Map;
 
 @Slf4j
@@ -29,9 +32,9 @@ public class YunMallGoodsController {
      * 提供给企云商城php-单个供货和取消供货
      */
     @PostMapping("/save")
-    public Boolean saveGoods(@Valid  @RequestBody GoodsSaveReq req){
+    public Boolean saveGoods(@Valid @RequestBody GoodsSaveReq req) {
         log.info("---/yunmall/goods/save传入的参数为{}---", JSONObject.toJSONString(req));
-        return goodsService.saveGoods(req.getGdId(),req.getOp());
+        return goodsService.saveGoods(req.getGdId(), req.getOp());
     }
 
     /**
@@ -49,15 +52,15 @@ public class YunMallGoodsController {
      */
     @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
     public Map<String, Object> search(
-            @RequestParam(required = false) Long shopId,
-            @RequestParam(required = false) String keyWord,
-            @RequestParam(required = false) String categorys,
-            @RequestParam(required = false) String pts,
-            @RequestParam(required = false, defaultValue = "10") Integer psize,
-            @RequestParam(required = false, defaultValue = "1") Integer pno,
-            @RequestParam(required = false, defaultValue = "1") Integer orderBy) {
+        @RequestParam(required = false) Long shopId,
+        @RequestParam(required = false) String keyWord,
+        @RequestParam(required = false) String categorys,
+        @RequestParam(required = false) String pts,
+        @RequestParam(required = false, defaultValue = "10") Integer psize,
+        @RequestParam(required = false, defaultValue = "1") Integer pno,
+        @RequestParam(required = false, defaultValue = "1") Integer orderBy) {
         log.info("---/yunmall/goods/search shopId={},keyWord={},categorys={},pts={},psize={},pno={},orderBy={}---",
-                shopId, keyWord, categorys, pts, psize, pno, orderBy);
+            shopId, keyWord, categorys, pts, psize, pno, orderBy);
         return goodsService.searchGoods(shopId, keyWord, categorys, pts, psize, pno, orderBy);
     }
 

+ 1 - 0
src/main/java/zs/payment/dao/yunmall/GoodsDao.java

@@ -43,6 +43,7 @@ public class GoodsDao extends MongoDBDao<Goods> {
     }
 
     /**
+     * Document是刻意为之,为了保证返回值跟原接口的数据结构一致
      * 供货商品搜索(对应 ptmall GoodsMongoDao.qiyueGoodsSearch)
      * 返回原始 Document,保留文档中已存的全部字段以便按既定结构透传。
      */

+ 0 - 4
src/main/resources/application.yml

@@ -4,10 +4,6 @@ server:
     port: 18816 #msPayment
     servlet:
         context-path: /pay2
-    tomcat:
-        # 允许查询串中出现原始的 [ ] { } | 等字符(RFC 3986 默认拒绝),兼容 pts/categorys=[1200000] 形式
-        relaxed-query-chars: "[,],{,},|"
-        relaxed-path-chars: "[,],{,},|"
 
 # Spring Profile 配置
 spring: