package org.hsweb.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.MediaType;
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 org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("/error")
public class DefaultErrorController implements ErrorController {

    @Autowired
    private ErrorAttributes errorAttributes;

    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        RequestAttributes requestAttributes = new ServletRequestAttributes(request);
        Map<String, Object> model = errorAttributes.getErrorAttributes(requestAttributes, true);
        int code = ((int) model.get("code"));
        response.setStatus(code);
        return new ModelAndView("error/" + response.getStatus(), model);
    }

    @RequestMapping
    @ResponseBody
    public Object error(HttpServletRequest request, HttpServletResponse response) {
        RequestAttributes requestAttributes = new ServletRequestAttributes(request);
        Map<String, Object> model = errorAttributes.getErrorAttributes(requestAttributes, true);
        int code = ((int) model.get("code"));
        response.setStatus(code);
        return model;
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}
@@ -1001,46 +1001,8 @@
 ping
-(produces = MediaType.TEXT_HTML_VALUE)
 %0A   
@@ -1449,24 +1449,110 @@
 questMapping
+(produces = %7BMediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE%7D)
 %0A    @Respon

修改produces，解决IE浏览器访问html也得到json的bug