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

ぃ、小莉子 提交于 2019-12-22 18:35:24

问题


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 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 put

consumes =MediaType.APPLICATION_JSON_VALUE

inside @RequestMapping annotation

What am I missing?


回答1:


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




回答2:


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.




回答3:


This is late too, but in RESTClient(mozilla addon), you can add Content-Type: application/json from the Headers dropdown menu.



来源:https://stackoverflow.com/questions/47593079/content-type-text-plaincharset-utf-8-not-supported-error-in-spring-boot-insid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!