在RestController类的Spring启动中,内容类型'text / plain; charset = UTF-8'不支持错误

问题描述 投票:0回答:2

我在spring boot应用程序中获得了以下@RestController:

@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 / RestClient(两个mozilla插件)​​发送请求时,我收到以下错误:

{“timestamp”:1512129442019,“status”:415,“error”:“不支持的媒体类型”,“消息”:“内容类型'text / plain; charset = UTF-8'不支持”,“path”:“ /费用/餐馆“}

此错误表明端点不支持Json内容,但我确实放了

consumes = MediaType.APPLICATION_JSON_VALUE

在@RequestMapping注释中

我错过了什么?

spring spring-boot utf-8 spring-restcontroller spring-web
2个回答
1
投票

迟到的回复,但我有同样的问题发布答案,它可能对某人有用,所以我安装了Postman,然后只是将你的Content-Type更改为application / json


-1
投票

这也是晚了,但在RESTClient(mozilla插件)​​中,您可以从Headers下拉菜单中添加Content-Type:application / json。

© www.soinside.com 2019 - 2024. All rights reserved.