【问题标题】:Error code 415 when forwarding a POST from an api to another将 POST 从 api 转发到另一个时出现错误代码 415
【发布时间】:2019-07-25 17:19:23
【问题描述】:

我正在将一个应用程序/json 类型从邮递员客户端发送到一个 java API,该 API 将所有请求转发到该案例的特定 API。 在这个具体案例中,我有一个登录 API,我想集中代码听到这个 JSON:

来自邮递员的 JSON

{
    "name": "random name",
    "password": "random passwd"
}

执行转发的 API

@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String redirectHttpRequest(HttpServletRequest request, @Value("${endpoint}") String newURL,
        HttpServletResponse response) throws IOException {

    try {

        RestTemplate restTemplate = new RestTemplate();
        String result = null;


        String body = IOUtils.toString(request.getReader());

        if (request.getMethod().equals("GET")) {
            // result = restTemplate.getForObject(redirectUrl.toString(), String.class);
        } else if (request.getMethod().equals("POST")) {
            result = restTemplate.postForObject(newURL, body, String.class);

        }
        return result;

    } catch (HttpClientErrorException e) {
        System.out.println(e);
        return "OLA";
    }

}

新的 URL 是另一个 API 的 URL(在这种情况下,它是 localhost:8080 并且来自 application.properties 文件)。

我已经通过邮递员测试了登录 API,它可以工作,但是当我尝试将其连接到该转发 API 时,我收到以下错误:

org.springframework.web.client.HttpClientErrorException: 415 null。

我想知道我做错了什么或另一种方法。

邮递员电话

第二个端点代码

传递给第二个端点的正文值

用户类

公共类用户{

private String name;

private String password;

private List<String> groups;

public User(String name, String password) {
    this.name = name;
    this.password = password;
    this.groups = new ArrayList<String>();
}

public User() {

}

public String getName() {
    return this.name;
}

public String getPassword() {
    return this.password;
}

public List<String> getGroups() {
    return this.groups;
}

public String toString() {
    return "User: " + this.name + "\nGroups: " + this.groups;
}

【问题讨论】:

  • 为了澄清的目的,两个端点都在同一台服务器上吗? localhost:8080 在你的情况下?
  • 不,它们在不同的服务器上。一个在 8080 上,另一个在 8082 上
  • 所以localhost:8082 上的第一个和localhost:8080 上的第二个?相同的服务器,但不同的端口
  • 转发的是localhost:8082/forward,登录的是localhost:8080/login。我可以在他们两个之间进行获取请求,但不能发帖,我不知道为什么。
  • 您能否将postForObject 调用时bodynewURL 中的值添加到您的帖子中,以确保它们看起来正确

标签: java rest spring-boot post


【解决方案1】:

问题是您收到了415 错误代码。这意味着您的/login 端点不期望您发送给他的有效负载类型,请参阅here

415(不支持的媒体类型)

415 错误响应表明 API 无法处理客户端提供的媒体类型,如 Content-Type 请求标头所示。例如,如果 API 只愿意处理格式为 application/json 的数据,则包含格式为 application/xml 的数据的客户端请求将收到 415 响应。

例如客户端上传图片为image/svg+xml,但服务器要求图片使用不同的格式。

我认为这是因为当您调用postForObject 时,您并没有告诉您有效负载的媒体类型。因此,不是单独发送 json 字符串,而是需要将其包装到一个 HttpEntity 中,该 HttpEntity 包含正文以及指定要转发的有效负载的媒体类型的标头。试试这个:

...
} else if (request.getMethod().equals("POST")) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
    HttpEntity<String> entity = new HttpEntity<>(body, headers);
    result = restTemplate.postForObject(newURL, entity, String.class);
}
...

【讨论】:

  • 错误依旧出现,body有我帖子的body,新的url是登录api运行的url,也就是localhost:8080/login,要不要打印body细绳?两者都消耗 MediaType.APPLICATION_JSON_UTF8_VALUE
  • 可以试试把body改成{"name":"random name", "password":"random passwd"}
  • 我已将我的邮递员信息放在我的问题上,这是你想要的吗?
  • 我的意思是您在正文中使用的 JSON 字符串:{ name:"random name", password"random passwd" } 不是有效的 JSON,所以这可能是问题所在。所以我希望您尝试使用有效的 JSON 字符串:{"name":"random name", "password":"random passwd"}
  • 我把我的字符串主体和你提供的信息放在一起,它仍然不起作用,同样的错误。postForObject 的第二个参数是我想在另一个 api 上使用的信息,对吗?跨度>
猜你喜欢
  • 1970-01-01
  • 2015-09-14
  • 2020-11-19
  • 2020-10-08
  • 2019-09-03
  • 2016-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
相关资源
最近更新 更多