在SpringBoot中使用@RestController注解相当于在具体方法中使用@ResponseBody注解,只能返回JSON格式数据
例如:
import com.alibaba.fastjson.JSONObject;//阿里的json工具
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("index")
public JSONObject index(){
JSONObject res = new JSONObject();
res.put("result","index");
}
}
解决方案:
import com.alibaba.fastjson.JSONObject;//阿里的json工具
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("index")
public void index(@RequestBody String type,HttpServletResponse response){
if(type == "json"){
JSONObject res = new JSONObject();
res.put("result","index");
response.getWriter().println(res);
}
if(type == "html"){
response.setHeader("Content-Type", " text/html;charset=UTF-8");
response.getWriter().println(html);//html = 实际的html内容
}
}
}