MessageConverter获取LinkedHashMap而不是常规对象作为输入

时间:2017-05-15 06:14:39

标签: java spring

我对春天的消息转换器有疑问。 我有跟随控制器的功能,应该在常规情况下返回JSON,但如果有异常应返回XML,所以我想编写自定义消息转换器:

@Controller
@RequestMapping("status")
public class StatusControler
{
@RequestMapping(value="/get", produces = {"text/json", "text/xml"}) 
@ResponseBody
public void getStatus(@RequestBody String reqBody, HttpServletRequest request, HttpServletResponse res)
     throws BaseAppException {
 /*
     Throws exception
 */
}

异常由ExceptionHandler处理(XMLResponse用javax @XML访问器注释):

@ControllerAdvice
public class AppExceptionHandler {
@ExceptionHandler(BaseAppException .class)
@ResponseBody
public XMLResponse handleBaseException(HttpServletResponse response, final BaseAppException ex) {
    response.setContentType("text/xml");
    XMLResponse ramloadResponse = new XMLResponse(ex);
    return ramloadResponse;
}

然后传递给我的消息转换器,后面有writeInternal(LOGGER是slf4j实例):

@Override
protected void writeInternal(Object t, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    LOGGER.info(t.getClass().toString());
}

现在在writeInternal中我得到LinkedHashMap,它是XMLResponse实例的JSON。有没有办法在这里获得XMLResponse?我想检查响应类,如果它是XMLResponse将它编组为xml,在任何其他情况下 - 将其编组为JSON

0 个答案:

没有答案