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

标签 spring spring-boot utf-8 spring-restcontroller spring-web

我在 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/RestedClient(mozila 的两个插件)发送请求时,我收到以下错误:

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



此错误指出终点不支持 Json 内容,但我做到了


consumes =MediaType.APPLICATION_JSON_VALUE



@RequestMapping注解内

我错过了什么?

最佳答案

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

关于spring - 内容类型 'text/plain;charset=UTF-8' 在 RestController 类中的 Spring Boot 中不支持错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47593079/

相关文章:

java - 如何在 Spring Kafka Listener 中捕获警告 "Broker may not be available"

Spring Boot Rest WebService,如何改进干净的代码?

java - MapStruct 不生成实现类

ruby-on-rails - rails + ruby 1.9 "invalid byte squence in US-ASCII"

python - 对 UTF-8 编码的日语语料库进行 R 处理

java - 如果 Java 应用程序作为服务运行,如何关闭 Spring 上下文?

DelegatingFilterProxy 上的 Spring 和 @Autowired

Spring Data-如何告诉spring要检索哪些实体

java - Spring Boot 抛出异常时不显示自定义错误

android - 在Android中解压unicode文件名错误