[英]spring boot rabbitmq MappingJackson2MessageConverter custom object conversion
[英]Custom implementation for MessageConverter for Spring boot REST application not working
我有一个使用Spring Boot的rest应用程序构建。 api请求对象是包含String和ASN1OctetString的自定义对象。 因此,我为消息转换器编写了自定义实现。
但是我无法将inputStream转换为包含String和ASN1OctetString的customVO对象。 如何将输入流转换为customVo对象?
我尝试使用ObjectInput ois = new ObjectInputStream(is);
,但出现类似标头无效的错误。 请让我知道我如何解决这个问题。
代码详细信息如下。 该应用程序具有如下控制器:
@RequestMapping(method = RequestMethod.POST, value = "/MyApp/postMessage", produces = "application/json;charset=UTF-8",consumes="ASN1OctetString/bytes;charset=UTF-8")
public DeferredResult<MyCustomVO> process(@Valid @RequestBody MyCustomVO myCustomVO, HttpServletRequest request){
//service code
}
请求bean如下:
import com.unboundid.asn1.ASN1OctetString;
public class MyCustomVO {
protected String actionDesc;
protected ASN1OctetString transId;
//setter getter methods
}
消息转换器的自定义实现:-
public class MyCustomVOConverter extends AbstractHttpMessageConverter<MyCustomVO> {
public MyCustomVOConverter() {
super();
}
public MyCustomVOConverter(org.springframework.http.MediaType supportedMediaType) {
super(supportedMediaType);
}
public MyCustomVOConverter(MediaType... supportedMediaTypes) {
super(supportedMediaTypes);
}
@Override
protected boolean supports(Class<?> clazz) {
return MyCustomVO.class.equals(clazz);
}
@Override
protected MyCustomVO readInternal(Class<? extends MyCustomVO> clazz, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
MyCustomVO MyCustomVORequest= new MyCustomVO();
InputStream is= httpInputMessage.getBody();
**//Need to convert the stream to customVO object**
}
@Override
protected void writeInternal(MyCustomVO MyCustomVOResp, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException {
}
}
您必须在messageConverter中指定MediaType,否则转换器不知道他负责哪种媒体类型。 执行以下操作来注册媒体类型:
public MyCustomVOConverter() {
MediaType type = new MediaType("ASN1OctetString", "bytes", Charset.forName("UTF-8");
setSupportedMediaTypes(Arrays.asList(type));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.