Spring @RestController 获取响应 json 或 html 的请求内容类型
Spring @RestController Get Request Content-Type to response json or html
如何获取请求的 Content-Type 值?我们需要它来打印 json 响应或 Html 响应。我的代码是这样的:
@RestController
public class GestorController {
@RequestMapping(value="/gestores", method = RequestMethod.GET)
public Object gestoresHtml(@RequestParam(value="name", required=false, defaultValue="sh14") String name) throws Exception {
String json = "prueba json";
String contentType = ?????
if(contentType.equals("application/json")){
return json;
}else{
ModelAndView mav = new ModelAndView();
mav.setViewName("gestores");
mav.addObject("name", name);
return mav;
}
}
}
谢谢大家。
如何获取请求的 Content-Type 值?我们需要它来打印 json 响应或 Html 响应。我的代码是这样的:
@RestController
public class GestorController {
@RequestMapping(value="/gestores", method = RequestMethod.GET)
public Object gestoresHtml(@RequestParam(value="name", required=false, defaultValue="sh14") String name) throws Exception {
String json = "prueba json";
String contentType = ?????
if(contentType.equals("application/json")){
return json;
}else{
ModelAndView mav = new ModelAndView();
mav.setViewName("gestores");
mav.addObject("name", name);
return mav;
}
}
}
谢谢大家。