Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring中编写一个REST端点,它只通过另一个API的响应发送?_Java_Spring - Fatal编程技术网

Java 如何在Spring中编写一个REST端点,它只通过另一个API的响应发送?

Java 如何在Spring中编写一个REST端点,它只通过另一个API的响应发送?,java,spring,Java,Spring,我有一个RESTAPI,然后是一个带有Spring后端的webapp。在Spring后端,我想创建一个路由,让我们称它为api proxy/get data,它进入api,让我们称它为api/get data,无论它得到什么HTTP响应,它都应该直接发送到webapp前端。现在,我的代码如下所示: @RequestMapping( value = "/api-proxy/get-data/{id}", method = RequestMethod.GET, produce

我有一个RESTAPI,然后是一个带有Spring后端的webapp。在Spring后端,我想创建一个路由,让我们称它为
api proxy/get data
,它进入api,让我们称它为
api/get data
,无论它得到什么HTTP响应,它都应该直接发送到webapp前端。现在,我的代码如下所示:

@RequestMapping(
    value = "/api-proxy/get-data/{id}",
    method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<String> getData(@PathVariable String id)
{
    RestTemplate restTemplate = new RestTemplate();
    String apiURL = "http://myapi.com/api/get-data/" + id;
    ResponseEntity<String> response = restTemplate.getForEntity(apiURL, String.class);
    return response;
}
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(errorHandler);
@RequestMapping(
value=“/api proxy/get data/{id}”,
method=RequestMethod.GET,
products=MediaType.APPLICATION\u JSON\u值
)
公共响应属性getData(@PathVariable字符串id)
{
RestTemplate RestTemplate=新RestTemplate();
字符串apiURL=”http://myapi.com/api/get-data/“+id;
ResponseEntity response=restemplate.getForEntity(apiURL,String.class);
返回响应;
}
我原以为这会起作用,但不幸的是,当
restemplate.getForEntity
获取404时,它不会返回带有404状态码的
ResponseEntity
,而是抛出异常。因此,我必须手动检查响应,并在catch块中构建它的副本,并且还要对500个服务器错误进行检查,等等


在春天,我想要什么样的行为?无论从上游API得到什么HTTP响应,我都希望将其准确地发送到客户机、头文件和所有文件。几乎像一个代理。

也许你应该尝试另一种
restemplate
方法。正如我在javadoc中读到的:

执行给定RequestEntity中指定的请求,并将响应作为ResponseEntity返回

因此,您可以尝试使用以下方法更新代码:

RequestEntity request = RequestEntity.get(new URI(apiURL));
ResponseEntity<String> response = restTemplate.exchange(apiURL, String.class);
RequestEntity-request=RequestEntity.get(新URI(APIRL));
ResponseEntity response=restTemplate.exchange(apirl,String.class);

您可以尝试添加自己的错误处理程序:

ResponseErrorHandler errorHandler = new ResponseErrorHandler() {

    @Override
    public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
        return false;
    }

    @Override
    public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
    }
};
然后像这样连接它:

@RequestMapping(
    value = "/api-proxy/get-data/{id}",
    method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<String> getData(@PathVariable String id)
{
    RestTemplate restTemplate = new RestTemplate();
    String apiURL = "http://myapi.com/api/get-data/" + id;
    ResponseEntity<String> response = restTemplate.getForEntity(apiURL, String.class);
    return response;
}
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(errorHandler);

这些错误不会被视为错误,只会转发给客户端。

请尝试以下代码,它将为您提供出错的异常

ResponseEntity<String> response = restTemplate.exchange(Url, 
                                                        HttpMethod.GET, 
                                                        null, 
                                                        new ParameterizedTypeReference<String>>(){});

    if (response.getStatusCode().is2xxSuccessful()) {
        return response.getBody();
    } else if (response.getStatusCode().is4xxClientError()) {
        throw new Exception("Exception while "+response.getBody());
    } else if (response.getStatusCode().is5xxServerError()) {
        throw new Exception("Bad request error "+response.getBody());
    } else {
        throw new Exception("Exception while querying service "+response.getBody()); 
    }
ResponseEntity response=restemplate.exchange(Url,
HttpMethod.GET,
无效的
新的参数化类型引用>(){});
if(response.getStatusCode().is2xxSuccessful()){
返回response.getBody();
}else if(response.getStatusCode().is4xxClientError()){
抛出新异常(“异常同时”+response.getBody());
}else if(response.getStatusCode().is5xxServerError()){
抛出新异常(“错误的请求错误”+response.getBody());
}否则{
抛出新异常(“查询服务时异常”+response.getBody());
}

您是否尝试转发请求?如下所示:
return“forward:http://myapi.com/api/get-data/“+id@lore我不明白。如果我只是从这个方法返回这个url,那么我的javascript客户端在与Spring服务器对话时将以字符串形式接收url。你到底在说我应该做什么?你可以使用java.net.HttpURLConnection中的方法;和java.net.URL来执行您的请求,而不是restTemplate,而不处理异常抱歉,错误的建议。。。我的建议不适合你的情况。