Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Content type 'text/plain;charset=UTF-8' not supported error in spring boot inside RestController class

Ask Question private ResturantExpensesRepo repo; @RequestMapping(value = "/expenses/restaurants",method = RequestMethod.POST,consumes =MediaType.APPLICATION_JSON_VALUE , headers = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public void hello(@RequestBody ResturantExpenseDto dto) Logger logger = LoggerFactory.getLogger("a"); logger.info("got a request"); ResturantExpenseEntity resturantExpenseEntity = new ResturantExpenseEntity(); resturantExpenseEntity.setDate(new Date(System.currentTimeMillis())); resturantExpenseEntity.setName(dto.getName()); resturantExpenseEntity.setExpense(dto.getExpense()); repo.save(resturantExpenseEntity);

When I try to send request from restClient/RestedClient (both addons of mozila) I get the following error :

"timestamp": 1512129442019, "status": 415, "error": "Unsupported Media Type", "message": "Content type 'text/plain;charset=UTF-8' not supported", "path": "/expenses/restaurants"

This eror states that the end point doesnt support Json content,But I did

consumes =MediaType.APPLICATION_JSON_VALUE

inside @RequestMapping annotation

What am I missing?

No the error didn't state the endpoint doesn't support JSON. It state that it doesn't support text/plain. Content Type of json is application/json. Specify content type in your mozilla addons as application/json and it should be fine Yannic Bürgmann Dec 1, 2017 at 12:53 I know but you said that the error states, that the endpoint does not support json. That is wrong. The error sais that it does nkt Support text/plain. So your request that you're sending client side has not the correct content-type Header. Yannic Bürgmann May 31, 2018 at 20:24 please try to request like this: curl -X PUT -H 'Content-Type: application/json' -i localhost:8080/spring-rest/api/employees/500 --data '{ "name": "abc", "email": "abc.a@gmail.com", "salary": 10000 }' prashant.kr.mod Sep 4, 2019 at 4:32

If the request is made like this: then it will resolve the issue.

curl -X PUT -H 'Content-Type: application/json' -i http://localhost:8080/spring-rest/api/employees/500 --data '{
  "name": "abc",
  "email": "abc.a@gmail.com",
  "salary": 10000

I see the headers are proper: headers = MediaType.APPLICATION_JSON_VALUE
but when the request is made, at that time we need to inform the handler that its a application/json mime type.

if you are using html with ajax.Check the request header and the payload. Make sure the ajax has the following fields

                url : your url
                type : 'post',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data : JSON.stringify( your payload )

if the ajax call has the following fields remove them and try again

         processData : false,
         contentType : false,
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.