我使用Spring启动构建了一个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
}
Request 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 {
}
}
答案 0 :(得分:0)
您必须在messageConverter中指定MediaType,否则转换器不知道他负责的媒体类型。 做这样的事情来注册mediatype:
public MyCustomVOConverter() {
MediaType type = new MediaType("ASN1OctetString", "bytes", Charset.forName("UTF-8");
setSupportedMediaTypes(Arrays.asList(type));
}