spring and cors不接受内容类型为application / json; charset = UTF-8

问题描述 投票:-1回答:2

我已使用Spring Boot创建REST API。我已启用CORS以允许我的角度应用程序发出http请求。通过Java配置启用Cors,如下所述:https://www.baeldung.com/spring-cors当我发出发布请求时,我收到415错误。此错误表明不支持内容类型'application / json; charset = UTF-8'。如果删除CORS的配置,它将与httpie和相同的标头一起使用。我不知道这个问题。我在版本2.2.5中使用spring boot。这是我的请求和响应的内容:要求:

POST /api/v1/users HTTP/1.1
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 107
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/2.1.0
accept: application/json

{
    "email": "[email protected]",
    "firstName": "John",
    "lastName": "Doe"
}

响应:

HTTP/1.1 415
Connection: keep-alive
Content-Type: application/json
Date: Sun, 24 May 2020 12:00:29 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers

{
    "apierror": {
        "debugMessage": "Content type 'application/json;charset=UTF-8' not supported",
        "message": "application/json;charset=UTF-8 media type is not supported. Supported media types are application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, multipart/mixed, application/json, application/*+json, */*",
        "status": "UNSUPPORTED_MEDIA_TYPE",
        "subErrors": null,
        "timestamp": "24-05-2020 12:00:29"
    }
}
spring-boot rest http content-type
2个回答
-1
投票

尝试使用内容类型'application / json',如果可行。


-1
投票

您必须在请求的标题中添加“ application / json”。像-“ accept:application / json”。

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