Setting Response Content-Type for JSON in Spring MVC (@ResponseBody)
Spring MVC's @ResponseBody annotation typically returns data in the form of plain text or XML. However, when handling JSON responses, you may need to ensure the correct content-type and charset are set to avoid encoding issues.
To set the response content-type explicitly, add the produces attribute to the @RequestMapping annotation, specifying the MIME type and charset you desire. For example, to specify JSON output with UTF-8 encoding:
@RequestMapping(value = "/getDealers", method = RequestMethod.GET, produces = "application/json; charset=utf-8") @ResponseBody public String sendMobileData() { }
This approach overrides the default content-type setting, ensuring that the response is returned as JSON with the desired encoding. By customizing the content-type and charset in this way, you can prevent encoding inconsistencies and ensure proper handling of JSON responses in your Spring MVC application.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3