**web.config**页面传参部分乱码,通过修改webconfig 解决问题

本文介绍了一种解决ASP.NET应用程序中使用URL传递中文参数时出现乱码的方法。通过配置Web.config文件中的globalization节点来指定编码方式,成功解决了接收页面上中文显示不全的问题。

问题描述:

用response.redirect("test.aspx?corp_kind ="国有或国有控股企业")传参

test.aspx:  用request.querystring ["corp_kind"]接收参数 ,国有或国有控股企业 变成了 国有或国有控股企□ 少了一个字,变成乱码

 

解决办法:

状态栏还是 国有或国有控股企业 看来问题出现在接收页面

通过在WEBCONFIG 的system.web 节点下 加一句 <globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312" />  问题解决

有一个这个文件,/* * Copyright (c) 2025, TP-Link. All rights reserved. */ package com.tplink.smb.common.data.management.system.config.webConfig; import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.support.config.FastJsonConfig; import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter; import com.tplink.smb.common.data.management.system.config.properties.FileProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; /** * @author Chen Jiayuan * @version 1.0 * @since 2025/9/30 */ @Configuration @EnableWebMvc public class ConfigurerAdapter implements WebMvcConfigurer { /** * 文件配置 */ private final FileProperties properties; public ConfigurerAdapter(FileProperties properties) { this.properties = properties; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.addAllowedOriginPattern("*"); config.addAllowedHeader("*"); config.addAllowedMethod("*"); source.registerCorsConfiguration("/**", config); return new CorsFilter(source); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { FileProperties.ElPath path = properties.getPath(); String avatarUtl = "file:" + path.getAvatar().replace("\\", "/"); String pathUtl = "file:" + path.getPath().replace("\\", "/"); registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0); registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0); registry .addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/resources/") .setCachePeriod(0); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // 配置 FastJsonHttpMessageConverter FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); List<MediaType> supportMediaTypeList = new ArrayList<>(); supportMediaTypeList.add(MediaType.APPLICATION_JSON); FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy-MM-dd HH:mm:ss"); // 开启引用检测 config.setWriterFeatures(JSONWriter.Feature.ReferenceDetection); converter.setFastJsonConfig(config); converter.setSupportedMediaTypes(supportMediaTypeList); converter.setDefaultCharset(StandardCharsets.UTF_8); converters.add(converter); } }
最新发布
10-30
你提供的代码是一个典型的 **Spring Boot 中通过 `@EnableWebMvc` 自定义 Web 配置** 的类,使用了 **Fastjson2** 作为 JSON 序列化工具。 但结合你之前的问题: > ❌ `HttpMessageNotWritableException: No converter for [SystemHealth] with Content-Type 'null'` 我们现在已经可以 **精准定位问题根源** 并给出解决方案。 --- ## ✅ 一、问题根本原因分析 ### 🔴 核心问题: ```java @Configuration @EnableWebMvc // ←← 这是罪魁祸首! public class ConfigurerAdapter implements WebMvcConfigurer { ... } ``` ### ⚠️ `@EnableWebMvc` 做了什么? 它会 **完全关闭 Spring Boot 的默认 MVC 自动配置**,包括: - 默认注册的 `Jackson2ObjectMapperBuilder` - `MappingJackson2HttpMessageConverter` - 其他内置的消息转换器(如 String、Form 等) 然后要求你 **手动配置所有 MVC 组件**。 而你在 `configureMessageConverters()` 方法中只添加了 Fastjson 转换器,**没有正确设置其支持的媒体类型(尤其是 `application/json`)和字符集等关键参数**,导致: 👉 `/actuator/health` 返回时无法序列化为 JSON → 抛出异常! --- ## ✅ 二、修复方案(推荐两种方式) --- ### ✅ 方案 1:【推荐】移除 `@EnableWebMvc`,保留自动配置 + 扩展 Fastjson 这是 **Spring Boot 最佳实践**! #### ✅ 修改后代码如下: ```java package com.tplink.smb.common.data.management.system.config.webConfig; import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.support.config.FastJsonConfig; import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter; import com.tplink.smb.common.data.management.system.config.properties.FileProperties; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; /** * @author Chen Jiayuan * @version 1.0 * @since 2025/9/30 */ @Configuration // 移除了 @EnableWebMvc ←← 关键!让 Spring Boot 自动配置生效 public class ConfigurerAdapter implements WebMvcConfigurer { private final FileProperties properties; public ConfigurerAdapter(FileProperties properties) { this.properties = properties; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.addAllowedOriginPattern("*"); config.addAllowedHeader("*"); config.addAllowedMethod("*"); source.registerCorsConfiguration("/**", config); return new CorsFilter(source); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { FileProperties.ElPath path = properties.getPath(); String avatarUtl = "file:" + path.getAvatar().replace("\\", "/"); String pathUtl = "file:" + path.getPath().replace("\\", "/"); registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0); registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0); registry.addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/resources/") .setCachePeriod(0); } /** * 提供 FastJsonHttpMessageConverter 给 Spring Boot 自动集成 */ @Bean public HttpMessageConverters customConverters() { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); // 设置支持的 media type List<MediaType> supportedMediaTypes = new ArrayList<>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); supportedMediaTypes.add(MediaType.valueOf("application/json;charset=UTF-8")); converter.setSupportedMediaTypes(supportedMediaTypes); // 配置 fastjson FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy-MM-dd HH:mm:ss"); config.setWriterFeatures(JSONWriter.Feature.ReferenceDetection); converter.setFastJsonConfig(config); // 设置默认字符集 converter.setDefaultCharset(StandardCharsets.UTF_8); return new HttpMessageConverters(converter); } } ``` ### ✅ 优点: - 不破坏 Spring Boot 默认行为 - Actuator、Jackson 默认转换器仍可用(即使你用了 Fastjson) - 更安全、更兼容 --- ### ✅ 方案 2:保留 `@EnableWebMvc`,但必须完整初始化消息转换器链 如果你坚持要完全控制 MVC 行为(比如性能调优、统一异常处理框架),那就必须自己补全所有缺失的部分。 #### ✅ 修改 `configureMessageConverters` 方法: ```java @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); // ✅ 必须明确添加 application/json 支持 List<MediaType> supportedMediaTypes = new ArrayList<>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); // 兼容旧版本 supportedMediaTypes.add(MediaType.valueOf("text/plain;charset=UTF-8")); supportedMediaTypes.add(MediaType.valueOf("application/x-www-form-urlencoded")); converter.setSupportedMediaTypes(supportedMediaTypes); FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy-MM-dd HH:mm:ss"); config.setWriterFeatures(JSONWriter.Feature.ReferenceDetection); converter.setFastJsonConfig(config); converter.setDefaultCharset(StandardCharsets.UTF_8); converters.add(converter); // ⚠️ 注意:当你重写 configureMessageConverters 时, // Spring Boot 不再添加其他默认转换器(如 String、Resource 等) // 如果需要,你需要手动添加或依赖后续 extendMessageConverters() } ``` > 💡 提示:你可以用 `extendMessageConverters()` 替代 `configureMessageConverters()` 来“追加”而非“替换”: > > ```java > @Override > public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { > // 插入到最前面 > converters.add(0, yourFastJsonConverter); > } > ``` 这样就不会丢失默认转换器。 --- ## ✅ 三、验证是否修复成功 重启服务后执行: ```bash curl -H "Accept: application/json" http://localhost:8091/actuator/health ``` 预期输出类似: ```json { "status": "UP", "components": { "diskSpace": { "status": "UP", "details": { ... } }, "ping": { "status": "UP" } } } ``` ✅ 成功返回 JSON → 说明消息转换器工作正常。 --- ## ✅ 四、最佳实践总结 | 原则 | 推荐做法 | |------|----------| | 🟢 普通项目 | **不要使用 `@EnableWebMvc`**,改用 `WebMvcConfigurer` 扩展 | | 🟡 高级定制 | 使用 `@EnableWebMvc` 可以,但必须确保所有 `HttpMessageConverter` 都被正确定义 | | 🟢 JSON 序列化 | 推荐 Jackson(生态最好),Fastjson2 也可以,注意版本安全 | | 🟢 Actuator 正常工作 | 确保有能处理 `application/json` 的 `HttpMessageConverter` | --- ## ✅ 五、额外建议:健康检查探针优化 在 K8s 中使用专用路径避免业务影响: ```yaml # application.yml management: endpoint: health: show-details: when_authorized endpoints: web: exposure: include: health,info,threaddump health: livenessstate: enabled: true readinessstate: enabled: true ``` 然后使用: - Liveness Probe: `/actuator/health/liveness` - Readiness Probe: `/actuator/health/readiness` 这两个端点由 Spring Boot 提供,不受数据库、Redis 是否正常的影响,更适合容器探针。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值