自定义HttpMessageConverter
HttpMessageConverter是用来处理request和response里的数据的。Spring内置了很多HttpMessageConverter,比如MappingJackson2HttpMessageConverter,StringHttpMessageConverter等。
package com.yidian.data.exptmgr.utils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.data.yidian.commons.utils.FastJsonUtils;
import com.yidian.data.exptmgr.entity.DumpSettings;
import com.yidian.data.exptmgr.entity.SettingInfo;
import com.yidian.data.exptmgr.model.exptsetting.SettingInfoRequest;
import org.apache.commons.io.Charsets;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.nio.charset.Charset;
import static com.yidian.data.exptmgr.enums.AppType.MODELDUMP;
import static com.yidian.data.exptmgr.enums.AppType.TRAINING;
/**
* @author wangyong
* @date 2019/1/30
*/
public class SettingConverter extends AbstractHttpMessageConverter<SettingInfoRequest> {
/**
* 定义字符编码,防止乱码
*/
private static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
/**
* 新建自定义的媒体类型
*/
public SettingConverter() {
super(new MediaType("application", "json", DEFAULT_CHARSET));
}
/**
* 表明只处理Settings这个类
*/
@Override
protected boolean supports(Class<?> aClass) {
return SettingInfoRequest.class.isAssignableFrom(aClass);
}
/**
* 重写readInternal方法,处理请求的数据
*/
@Override
protected SettingInfoRequest readInternal(Class<? extends SettingInfoRequest> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
final String temp = StreamUtils.copyToString(httpInputMessage.getBody(), DEFAULT_CHARSET);
if (temp.contains(TRAINING.name())) {
return JSONObject.parseObject(temp, new TypeReference<SettingInfoRequest<SettingInfo>>(){});
} else if (temp.contains(MODELDUMP.name())) {
return JSONObject.parseObject(temp, new TypeReference<SettingInfoRequest<DumpSettings>>(){});
}
return null;
}
/**
* 重写writeInternal,处理如何输出数据到response
*/
@Override
protected void writeInternal(SettingInfoRequest request, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException {
final String out = FastJsonUtils.toJSONString(request);
StreamUtils.copy(out, DEFAULT_CHARSET, httpOutputMessage.getBody());
}
}
配置自定义Converter
package com.yidian.data.exptmgr.config;
import com.yidian.data.exptmgr.utils.SettingConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.List;
/**
* WebMVC配置
* @author wangyong
* @date 2018/11/12
* @since 0.0.1-SNAPSHOT
*/
@Configuration
public class AlphaWebConfig extends WebMvcConfigurerAdapter {
/**
* 添加自定义的httpMessageConverter
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(messageConverter());
}
@Bean
public SettingConverter messageConverter(){
return new SettingConverter();
}
}
网友评论