1、问题:ssm框架中在controller层使用@ResponseBody注解,一般会跨过视图渲染,直接在客户端显示json的数据格式,但自己的项目总是报406错误!郁结
2、为解决前效果图:
image.png
3、解决后效果图:
image.png
4、将返回的数据以json的数据形式转换为字符串,使用的com.alibaba.fastjson.JSON:jar包截图如下:
image.png
5、代码如下:
package com.jure.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jure.service.impl.QRcodeService;
@Controller
@RequestMapping("/wx")
public class QRcodeController {
/**
* 接收二维码
* @param request
* @return
* @throws IOException
*/
//@RequestMapping(value="/QRCode",method=RequestMethod.GET,produces="text/html;charset=utf-8")
@RequestMapping(value="/QRCode",method=RequestMethod.GET,produces="text/json;charset=utf-8")
@ResponseBody
public String twoCode(HttpServletRequest request) throws IOException{
JSONObject data=new JSONObject();
String accessToken = QRcodeService.getToken();
System.out.println("accessToken;"+accessToken);
String twoCodeUrl = QRcodeService.getminiqrQr(accessToken,request);
System.out.println("twoCodeUrl:"+twoCodeUrl);
data.put("twoCodeUrl", twoCodeUrl);
return JSON.toJSONString(data);
}
}
做一下笔记!仅为自己的随笔!