JAVA SprimgMVC JSON returns Chinese question mark

problem description

controller of the request

@ControllerAdvice
@ResponseBody
public class ControllerException {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(BindException.class)
    public String bindException(Exception e) {
        List<ObjectError> eList = ((BindException) e).getBindingResult().getAllErrors();
        String errStr = "";
        for(int i=0; i< eList.size(); iPP) {
            errStr += ((FieldError) eList.get(i)).getField() + " " + eList.get(i).getDefaultMessage() + " ";
        }
        return resultError(e, CodeEnum.CODE_1402, errStr);
    }

    
    
    /**
     * 
     * @param e 
     * @param c 
     * @return
     */
    private String resultError(Exception e, CodeEnum c) {
        logger.warn(e.getMessage());
        logger.warn(c.getMessage());
        return JsonUtil.formatJSON(c, e.getMessage());
    }

}

related codes

in addition, I added -Dfile.encoding=utf-8

to the startup of tomcat.

what result do you expect? What is the error message actually seen?

when an exception occurs, the request to the controller object verification fails to return. What else do you need to configure so that the global return of json Chinese will not be a problem?

Nov.25,2021

add a produces parameter to @ RequestMapping:
produces = "application/json; charset=UTF-8"

Menu