StringMVC返回字符串

复制代码
@RequestMapping(value="twoB.do")
public void twoBCode(HttpServletRequest request,HttpServletResponse response) {
    //.......... 此处省略 N行
    try {
        response.setContentType("type=text/html;charset=UTF-8");
        String s = "一堆字符串......";
        response.getWriter().write(s);
    } catch (Exception e) { 
        e.printStackTrace();
    }
    return;
}
复制代码

SpringMVC返回字符串的方法有好多,来个简单粗暴的方法:

使用@ResponseBody 注解返回字符串

复制代码
 @RequestMapping(value="getJosn.do", produces="text/html;charset=UTF-8")
 @ResponseBody
 public String getTabJson(){
     String json = "{"无主题":"http://www.wuzhuti.cn"}";
     return json;
 }
复制代码
posted @ 2017-08-20 09:00  执念哈i  阅读(650)  评论(0编辑  收藏  举报