【问题标题】:Content type 'text/plain;charset=UTF-8' not supported error in spring boot inside RestController class内容类型'text/plain;charset = UTF-8'在RestController类中的spring boot中不支持错误
【发布时间】:2018-05-15 12:58:31
【问题描述】:

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

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

这个错误表明端点不支持Json内容,但我做到了 放

使用 =MediaType.APPLICATION_JSON_VALUE

@RequestMapping 注解内

我错过了什么?

【问题讨论】:

  • 否,错误并未说明端点不支持 JSON。它声明它不支持文本/纯文本。 json 的内容类型是 application/json。在你的 mozilla 插件中指定内容类型为 application/json 应该没问题
  • @YannicKlem 我无法更改客户端如何支持此 Mediatype 服务器端。
  • 我知道,但您说错误表明端点不支持 json。那是错的。错误说它确实 nkt 支持 text/plain。因此,您发送客户端的请求没有正确的内容类型标头。
  • 请尝试这样请求: curl -X PUT -H 'Content-Type: application/json' -i localhost:8080/spring-rest/api/employees/500 --data '{ "name": "abc", "email ": "abc.a@gmail.com", "薪水": 10000 }'

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


【解决方案1】:

如果您使用带有 ajax 的 html。请检查请求标头和有效负载。确保 ajax 有以下字段

                url : your url
                type : 'post',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data : JSON.stringify( your payload )

如果 ajax 调用有以下字段,请删除它们并重试

         processData : false,
         contentType : false,

【讨论】:

    【解决方案2】:

    这也太晚了,但是在 RESTClient(Mozilla 插件) 中,您可以从 Headers 下拉菜单中添加 Content-Type: application/JSON 甚至在响应端将其更改为 JSON 格式

    【讨论】:

      【解决方案3】:

      如果请求是这样的:那么它将解决问题。

      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 类型。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2023-02-15
          • 2014-09-18
          • 1970-01-01
          • 2019-11-23
          • 2018-08-06
          • 2019-12-16
          • 2018-08-19
          • 2020-04-13
          • 2019-12-22
          相关资源
          最近更新 更多