你可以注入傑克遜的ObjectMapper到控制器通過一個ResponseEntity返回之前的結果手動序列化到JSON
。
@Configuration
public class Config {
@Bean
public ObjectMapper objectMapper() {
// returning a plain ObjectMapper,
// you can change this to configure the ObjectMapper as requiered
return new ObjectMapper();
}
}
@Controller
@RequestMapping("/Index")
public class ControllerClass {
@Autowired
private ObjectMapper objectMapper;
@RequestMapping(value="/Result",
method=RequestMethod.GET,
produces="application/json")
@ResponseBody
public ResponseEntity<String> result(){
List<Integer> result = new ArrayList<Integer>();
result.add(1);
result.add(2);
String jsonResult = objectMapper.writer().writeValueAsString(result);
// here you can store the json result before returning it;
return new ResponseEntity<String>(jsonResult, HttpStatus.OK);
}
}
編輯:
您也可以嘗試定義HandlerInterceptor捕獲響應主體對你有興趣的請求。
@Component
public class RestResponseInterceptor implements HandlerInterceptor {
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
// inspect response, etc...
}
}
我不明白你的問題。你想要做什麼?你在申請annotations_之後是什麼意思_? – 2014-09-06 17:13:19