1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package com.fjhx.email.config;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.fasterxml.jackson.core.JsonGenerator;
- import com.fasterxml.jackson.databind.JsonSerializer;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.SerializerProvider;
- import com.fasterxml.jackson.databind.module.SimpleModule;
- import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
- import com.fjhx.email.utils.PageWrapper;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.converter.HttpMessageConverter;
- import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.List;
- /**
- * WebMvc配置
- */
- @EnableWebMvc
- @Configuration
- @SuppressWarnings({"unchecked", "rawtypes"})
- public class WebMvcConfig implements WebMvcConfigurer {
- /**
- * webMvc序列化
- */
- @Override
- public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
- MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
- ObjectMapper objectMapper = converter.getObjectMapper()
- // 时间格式化
- .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
- .registerModule(new SimpleModule()
- // 将Long转换成String
- .addSerializer(Long.class, ToStringSerializer.instance)
- // 将Page转成PageWrapper
- .addSerializer(IPage.class, new JsonSerializer<IPage>() {
- @Override
- public void serialize(IPage page, JsonGenerator jg, SerializerProvider sp) throws IOException {
- jg.writeObject(new PageWrapper(page));
- }
- })
- );
- converter.setObjectMapper(objectMapper);
- converters.add(converter);
- }
- }
|