❶ fastjson 過濾多餘的欄位!
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(實體類.class, "欄位名回","欄位名");
答 JSON.toJSONString(list, filter);
❷ Spring特殊字元處理
在使用Spring或Spring Boot時一些特殊的參數會被轉義,或者因轉義導致出現異常情況,本文匯總總結相關問題及解決方案,幫助大家快速定位和解決問題。
問題一:參數特殊符號被後端轉義
WEB開發時,前端通過get/post方法傳遞參數的時,如果實參附帶特殊符號,後端接收到的值中特殊符號就會被轉義。
例如請求: http://localhost:10001/demo/index?name= 張三(1)
後端接收到的name值中「(」和「)」被轉義。
針對此問題有以下解決方案:
1、檢查web.xml里是否配置了過濾特殊字元的filter,若不需要可以關掉此filter。
2、java中可以使用org.apache.commons.lang包中的public static String unescapeHtml(String str)方法來進行解碼。實踐中嘗試了很多方法沒有解決,最後使用了該方法,將接收到的包含特殊字元的字元串通過該方法進行解碼。
3、在Controller接收的參數前加上@RequestBody註解,示例如下:
@PostMapping(value = "/add")
@ResponseBody
public String addMessage(@RequestBody ParamVo params) {
}
通常情況下,基於RESTful的API經常使用@RequestBody來自動綁定body中的請求參數到實體類對象。使用@RequestBody能解決大多數情況的問題,但某些特殊字元依舊無法正常解決,還需要通過方案二進行補充解決。
使用該中方案進行數據交互時,前度對應的請求需要 聲明dataType和contentType,傳遞的參數並用JSON.stringify()轉為json字元串。
$.ajax({
url: CONTEXTPATH + "/add",
type: 'POST',
dataType: 'JSON',
contentType : 'application/json',
data: JSON.stringify(Data),
success: function (data) {
}
})
問題二:/被轉義成%2F導致400錯誤
前端GET請求url中帶有路徑參數,參數中有/特殊字元,前端已經轉義成了%2F,後端springboot並沒有收到這個請求,直接返回了400的錯誤。
原因:據說是tomcat默認是不支持轉義的,需要手動設置一下轉化,搜索tomcat的設置可以找到,但在springboot中內置的tomcat,在yml中找不到相關的配置。
解決方案:修改啟動類,添加系統參數並重寫WebMvcConfigurerAdapter的configurePathMatch方法。
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) throws Exception {
System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
SpringApplication.run(Application.class, args);
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
其中實現WebMvcConfigurerAdapter介面新版本中改為實現WebMvcConfigurer介面。重寫的方法名稱是一樣的。該部分也可以單獨在WebMvc的配置類中實現,而不是放在啟動類。
當然,設置tomcat的參數是需要寫在main方法當中進行設置的。
問題三:整合jackson的轉義
Spring Boot默認配置json轉換工具就是Jackson, 如果你此時使用的正是Jackson框架,那麼可在配置文件中進行是否轉義的配置,配置項如下:
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.serialization.indent_output=true
spring.jackson.serialization.fail_on_empty_beans=false
spring.jackson.defaultPropertyInclusion=NON_EMPTY
spring.jackson.deserialization.fail_on_unknown_properties=false
spring.jackson.parser.allow_unquoted_control_chars=true
spring.jackson.parser.allow_single_quotes=true
其中重點關注allow_unquoted_control_chars項的配置。
當然,相應的配置如果是在配置類中實現的自定義ObjectMapper,可以在自定義時進行設置:
@Configuration
public class JacksonConfig {
}
在類中做過修改中, 配置文件中的配置將不再起作用。
問題四:接收JSON時發生轉義字元綁定對象失敗
在做Spring boot 項目時發生json轉義字元綁定對象失敗,原因是json裡面有些欄位包括空格,反斜杠等,如果框架沒有對這些json進行轉化時,就會報類似如下錯誤
org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 9)): has to be escaped using backslash to be included in string value
at [Source: java.io.StringReader@10cfc2e3 ; line: 1, column: 2461]
解決辦法:
1、pom.xml文件增加fastjson依賴。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
如果項目中已經引入則無需新增該依賴。
2、增加配置類
@SpringBootApplication
public class JsonController extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
這里的配置方法同問題二中的一樣,新版本Spring Boot 通過WebMvcConfigurer介面來完成。
❸ java中怎麼過濾沒有用的欄位返回到前台的數據
1 不用框架
傳統方式用jdbc讀取資料庫數據到resultset,
a 通過json工具內:將resultset中數據寫到容list里,通過json工具(如fastjson)轉換成json字元串,返回到前台
b 直接通過resultset吧數據拼寫成json格式字元串寫到前台
2 框架
如struts2+spring, jdbctemple支持將讀取結果直接返回給list, 將result類型設置生json即可,前台可直接獲取json數據