Spring Web MVC
控制器响应消息体
@ResponseBody
这是指定响应的消息体
字符串
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
图片
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
json
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
例子
例子1:返回验证码
@ResponseBody
@GetMapping(value = "/code", produces = MediaType.IMAGE_PNG_VALUE)
public Image generateVC(HttpServletRequest req, HttpServletResponse resp) {
String code = CommonUtil.verifyCodeStr(4);
BufferedImage image = CommonUtil.codeStrImage(code, 72, 20);
req.getSession().setAttribute("code", code);
return image;
}