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

7

21

I got the following @RestController inside a spring boot application :

@Data
@RestController
public class Hello {

    @Autowired
    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 a 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 error states that the endpoint doesn't support Json content, but I did put

consumes =MediaType.APPLICATION_JSON_VALUE

inside @RequestMapping annotation

What am I missing?

Tetchy answered 1/12, 2017 at 12:7 Comment(4)
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 fineEastbourne
@YannicKlem I can't change client side how to support this Mediatype server side.Pro
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.Eastbourne
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": "[email protected]", "salary": 10000 }'Visual
M
26

Late response but I had the same problem posting the answer it might be useful to someone so I installed Postman and then just change your Content-Type to application/json

Makepeace answered 6/9, 2018 at 18:9 Comment(0)
V
6

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": "[email protected]",
  "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.

Visual answered 4/9, 2019 at 4:34 Comment(0)
R
2

It is caused by the header 'Content-Type' NOT being set to 'application/json'. If you are using Postman as your REST client then follow the below image to fix the issue.

enter image description here

Rossanarosse answered 12/12, 2023 at 21:52 Comment(0)
R
1

This is late too, but in RESTClient(Mozilla addon), you can add Content-Type: application/JSON from the Headers dropdown menu and even at the response side change it to JSON format

Rolandorolandson answered 25/9, 2018 at 17:27 Comment(0)
M
0

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,
Marlea answered 3/12, 2020 at 14:4 Comment(0)
B
0

If you are trying to test this API through Postman,
-- Go to the Headers tab of Postman and
-- Click on Hidden headers(you will see this info at the top)
-- uncheck on Content-Type
-- Add new header information as;
Content-Type(key) and application/json(value)
-- Test the API, you will get the result

Boyle answered 12/10, 2023 at 11:49 Comment(0)
T
0

it will resolve the issue: adds to the header of your file.propretie:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo_db?sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&jdbcCompliantTruncation=false
Thusly answered 12/12, 2023 at 15:20 Comment(1)
Welcome to Stack Overflow! Thank you for your answer. Please provide more details about your solution. Code snippets, high quality descriptions, or any relevant information would be great. Clear and concise answers are more helpful and easier to understand for everyone. Edit your answer with specifics to raise the quality of your answer. For more information: How To: Write good answers. Happy coding!Secretin

© 2022 - 2024 — McMap. All rights reserved.