package com.star.msgconverter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.StreamUtils;
import org.springframework.util.TypeUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.star.model.User;
import com.star.output.OutputDto;
public class MessageConverter<T> extends MappingJackson2HttpMessageConverter {
private static final Logger logger = LoggerFactory.getLogger(MessageConverter.class);
private ObjectMapper jsonMapper = new ObjectMapper();
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
Class<?> targetClass = (type instanceof Class<?> ? (Class<?>) type : null);
if (targetClass == null) {
//获取type对应的可转换类型
ResolvableType resolvableType = ResolvableType.forType(type);
//Type对象转换为Class对象
targetClass = resolvableType.resolve();
}
logger.info("参数type:" + type.toString());
logger.info("参数contextClass:" + contextClass.toString());
logger.info("参数inputMessage:" + inputMessage.toString());
return readInternal(targetClass, inputMessage);
}
@Override
protected Object readInternal(Class<? extends Object> classzz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotWritableException {
if (TypeUtils.isAssignable(User.class, classzz) || TypeUtils.isAssignable(Map.class, classzz)) {
logger.info("自定义报文解析{}", classzz);
byte[] b = StreamUtils.copyToByteArray(inputMessage.getBody());
JsonNode master = jsonMapper.readTree(b);
JsonNode request = master.get("REQUEST");
if (request == null) {
logger.info("入参参数中缺少REQUEST节点,无法解析,请核对!");
}
else {
JsonNode data = request.get("REQUESTDATA");
JsonNode desc = request.get("REQUESTDESC");
if (data == null) {
logger.info("入参参数中缺少REQUESTDATA子节点,无法解析,请核对!");
}
else if(desc==null) {
logger.info("入参参数中缺少REQUESTDESC节点,无法解析,请核对!");
}
else {
try {
JSONObject jsonObject =JSONObject.parseObject(data.toString());
User user = jsonObject.toJavaObject(User.class);
if(user.getId()==null||user.getName()==null||user.getPassword()==null||user.getBirth()==null||user.getPhone()==null||user.getDepartment()==null) {
throw new Exception("请求参数数据格式有误!");
}else {
logger.info("------入参报文------");
logger.info(JSON.toJSONString(jsonMapper.treeToValue(data, classzz)));
return jsonMapper.treeToValue(data, classzz);
}
} catch (com.fasterxml.jackson.databind.exc.InvalidFormatException e) {
logger.info("请求参数格式错误");
} catch (UnrecognizedPropertyException e) {
logger.info("请求中包含未能识别的业务参数名", e.getMessage());
} catch (JsonProcessingException e) {
logger.info("错误OpenAPI请求!");
} catch (Exception e) {
logger.info("请求参数格式有误!");
}
}
}
return null;
} else {
logger.info("进入默认报文解析{}", classzz);
return super.readInternal(classzz, inputMessage);
}
}
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
if (TypeUtils.isAssignable(OutputDto.class, object.getClass())) {
OutputDto o = (OutputDto) object;
String str = JSON.toJSONString(o);
JSONObject json = JSONObject.parseObject(str);
Map<String,Object> returnMap = new HashMap<String,Object>();
Map<String,Object> map = new HashMap<String,Object>();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
map.put("RETURNDATA", o);
map.put("RETURNCODE", "SUCCESS");
map.put("RETURNDESC", type);
map.put("RETURNTIME",sf.format(new Date()));
returnMap.put("RESPONSE", map);
logger.info("------------------出参报文---------------");
logger.info(returnMap.toString());
super.writeInternal(returnMap, type, outputMessage);
} else {
super.writeInternal(object, type, outputMessage);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
springboot.rar (40个子文件)
springboot
.project 1KB
.springBeans 473B
mvnw.cmd 5KB
mvnw 6KB
src
test
java
com
star
springboot
SpringbootApplicationTests.java 340B
main
resources
application.properties 706B
mybatis
UserMapper.xml 398B
templates
spring
applicationContext.xml 7KB
static
java
com
star
service
UserService.java 392B
SpringbootApplication.java 470B
msgconverter
MessageConverter.java 5KB
output
OutputDto.java 611B
AppConfig.java 1002B
model
User.java 1KB
controller
UserController.java 2KB
mapper
UserMapper.java 136B
target
classes
application.properties 706B
com
star
service
UserService.class 714B
SpringbootApplication.class 799B
msgconverter
MessageConverter.class 7KB
output
OutputDto.class 1KB
model
User.class 2KB
controller
UserController.class 2KB
mapper
UserMapper.class 186B
AppConfig.class 1KB
META-INF
MANIFEST.MF 338B
maven
com.star
springboot
pom.properties 235B
pom.xml 3KB
mybatis
UserMapper.xml 398B
spring
applicationContext.xml 7KB
test-classes
com
star
springboot
SpringbootApplicationTests.class 646B
.settings
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.m2e.core.prefs 97B
org.eclipse.jdt.core.prefs 243B
org.eclipse.core.resources.prefs 155B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
pom.xml 3KB
.classpath 1KB
.gitignore 249B
共 40 条
- 1
资源评论
Starbucks_star
- 粉丝: 14
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功