首先除去json包,然后引入Gson的依赖
然后在配置类中做如下配置:
package com.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
/**
* @author juge
* @version 1.0
* @date 2020/5/30 20:42
*/
@Configuration
public class WebMvcConfig {
@Bean
GsonHttpMessageConverter gsonHttpMessageConverter(){
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
GsonBuilder gb = new GsonBuilder();
Gson gson = gb.setDateFormat("yyyy/MM/dd").create();
converter.setGson(gson);
return converter;
}
}
或者:
package com.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
/**
* @author juge
* @version 1.0
* @date 2020/5/30 20:42
*/
@Configuration
public class WebMvcConfig {
@Bean
GsonBuilder gsonBuilder(){
GsonBuilder gb = new GsonBuilder();
gb.setDateFormat("yyyy/MM/dd").create();
return gb;
}
}