1硼瓣、問題:ssm框架中在controller層使用@ResponseBody注解,一般會(huì)跨過(guò)視圖渲染置谦,直接在客戶端顯示json的數(shù)據(jù)格式堂鲤,但自己的項(xiàng)目總是報(bào)406錯(cuò)誤!郁結(jié)
2媒峡、為解決前效果圖:
image.png
3瘟栖、解決后效果圖:
image.png
4、將返回的數(shù)據(jù)以json的數(shù)據(jù)形式轉(zhuǎn)換為字符串谅阿,使用的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);
}
}
做一下筆記!僅為自己的隨筆签餐!