RestController类内的Spring Boot中不支持内容类型'text/plain; charset = UTF-8' [英] Content type 'text/plain;charset=UTF-8' not supported error in spring boot inside RestController class

查看:1932
本文介绍了RestController类内的Spring Boot中不支持内容类型'text/plain; charset = UTF-8'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Boot应用程序中获得了以下@RestController:

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);
    }
}

当我尝试从restClient/RestedClient(mozila的两个附加组件)发送请求时,出现以下错误:

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

{ 时间戳记":1512129442019, 状态":415, 错误":不支持的媒体类型", "message":内容类型为'text/plain; charset = UTF-8'不支持", 路径":"/费用/餐厅" }

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

这个错误指出端点不支持Json内容,但我确实做到了 放

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

使用= MediaType.APPLICATION_JSON_VALUE

consumes =MediaType.APPLICATION_JSON_VALUE

内部@RequestMapping注释

inside @RequestMapping annotation

我想念什么?

推荐答案

如果这样发出请求:那么它将解决问题.

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
}'

我看到标题是正确的:headers = MediaType.APPLICATION_JSON_VALUE
但是在发出请求后,那时我们需要通知处理程序它是application/json mime类型.

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.

这篇关于RestController类内的Spring Boot中不支持内容类型'text/plain; charset = UTF-8'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆