Java Restful style API defines the input parameter and return value encoding of the interface UTF-8

The project provides an external interface, but the return value is found to be garbled in Chinese!
The reason is that the external system stipulates that the input parameters and return value of the called interface must be in UTF-8 encoded JSON format, so the interface definition must be processed as follows:

@RequestMapping(value = "/doAAA", method = RequestMethod.POST,headers = "Content-type=application/json;charset=UTF-8",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String doApproval(@RequestBody MobileApprovalDoAssistParameter doAssistParameter) throws Exception {
    
    
        log.info("***********receive doAssist json:"+ JSONObject.toJSONString(doAssistParameter));
        MobileApprovalResponseDTO mobileApprovalResponseDTO = iMobileApprovalService.doAssist(doAssistParameter);
        return JSONObject.toJSONString(mobileApprovalResponseDTO);

    }

Note: The above is a self-encapsulated interface, which defines that the input parameter is in json format and is encoded in utf-8, and the return value is defined as text/html;charset=utf-8 , because the object has been manually converted into JSON format in the code String, so you can directly define the text/html format. If the method returns an object, it can be defined as application/json;charset=UTF-8 , then when the method returns, it will automatically convert it to json for us. This is based on your own In actual use, just deal with it yourself.
The input parameter is the encoding: headers = "Content-type=application/json;charset=UTF-8"
Return value: produces = "text/html;charset=utf-8"

Guess you like

Origin blog.csdn.net/wujian_csdn_csdn/article/details/106815542