Java Spring boot REST应用程序的MessageConverter的自定义实现不工作
我使用SpringBoot构建了一个rest应用程序。api请求对象是一个包含字符串和ASN1OctetString的自定义对象。因此,我为消息转换器编写了一个自定义实现 但我无法将inputStream转换为包含String和Asn1OctetString的customVO对象。 如何将输入流转换为customVo对象 我尝试使用ObjectInput ois=new ObjectInputStreamis;,但它给出了一个错误,比如header无效。 请告诉我如何解决这个问题 下面给出了代码的细节。 该应用程序具有如下控制器:-Java Spring boot REST应用程序的MessageConverter的自定义实现不工作,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我使用SpringBoot构建了一个rest应用程序。api请求对象是一个包含字符串和ASN1OctetString的自定义对象。因此,我为消息转换器编写了一个自定义实现 但我无法将inputStream转换为包含String和Asn1OctetString的customVO对象。 如何将输入流转换为customVo对象 我尝试使用ObjectInput ois=new ObjectInputStreamis;,但它给出了一个错误,比如header无效。 请告诉我如何解决这个问题 下面给出了代码
@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
}
消息转换器的自定义实现:-
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));
}
public MyCustomVOConverter() {
MediaType type = new MediaType("ASN1OctetString", "bytes", Charset.forName("UTF-8");
setSupportedMediaTypes(Arrays.asList(type));
}