Spring Boot 如何向其他人发送响应 URL

SpringBoot how to Send response to other URL

我有以下代码:

@RequestMapping(
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE},
            path = "api/api1",
            method = RequestMethod.POST,
            produces = MediaType.ALL_VALUE
    )
    public ResponseEntity<?> api1CallBack(@RequestBody String requestBody, HttpServletRequest request) throws IOException, GeneralSecurityException, URISyntaxException {
       String response="{SOME_JSON}";
        URI callbackURL = new URI("http://otherAPIEnv/api2");
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setLocation(callbackURL);
        return new ResponseEntity<String>(response,httpHeaders, HttpStatus.OK);
    }

我尝试了上面的代码,但是当我通过我的 curl 访问 api1 时,我在同一台机器上得到了响应,但我希望响应被重定向到其他 APIEnv 机器上的 api2。

有人可以建议如何实现这种请求和响应吗?

看起来像是 redirect 的工作。

String redirectMe() {
    return "redirect:http://otherAPIEnv/api2"
}

至于卷曲。您有方法的 POST 映射,因此请务必尝试使用 curl -X POST... 或将其更改为 GET.

您需要使用 redirect 并修改您方法的 return 类型

public String api1CallBack(@RequestBody String requestBody, HttpServletRequest request) throws IOException {
       return "redirect:http://otherAPIEnv/api2";
}

When you send a request to a URL it should respond to the same otherwise client will be in waiting for it until it times out.

因此,在这种情况下,方法应该有所不同。

首先,在你的主要休息API你必须发送一个响应代码来释放客户端。

然后,在 API 方法中,您必须异步调用另一个方法,该方法调用 api2 并执行所需的操作。

这是一个简单的例子。

@Autowired
API2Caller api2Caller;

@RequestMapping(
        consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE},
        path = "api/api1",
        method = RequestMethod.POST,
        produces = MediaType.ALL_VALUE
)
@ResponseStatus(HttpStatus.ACCEPTED)
public void api1CallBack(@RequestBody String requestBody, HttpServletRequest request) throws IOException, GeneralSecurityException, URISyntaxException {
    api2Caller.callApi2(requestBody);
}

并且 API调用者应该如下所示

@Component
public class API2Caller {

    @Async
    public SomeResultPojo callApi2() {
        // use RestTemplate to call the api2 
        return restTemplate.postForObject("http://otherAPIEnv/api2", request, SomeResultPojo.class);
    }
}

但是你可以选择你最舒服的方式来执行异步操作。

这是执行此类操作的更模块化、更通用的方法:

public @ResponseBody ClientResponse updateDocStatus(MyRequest myRequest) {
    ClientResponse clientResponse = new ClientResponse(CTConstants.FAILURE);
    try {
        HttpHeaders headers = prepareHeaders();
        ClientRequest request = prepareRequestData(myRequest);
        logger.info("cpa request is " + new Gson().toJson(request));
        HttpEntity<ClientRequest> entity = new HttpEntity<ClientRequest>(request, headers);
        String uri = cpaBaseUrl + updateDocUrl ;    
        ClientResponse serviceResponse = Utilities.sendHTTPRequest(uri, entity);
        clientResponse = serviceResponse;
        if (serviceResponse != null) {
            if (CTConstants.SUCCESS.equalsIgnoreCase(serviceResponse.getStatus())) {
                clientResponse.setStatus(CTConstants.SUCCESS);
                clientResponse.setMessage(" update success.");
            }
        }
    } catch (Exception e) {
        logger.error("exception occurred ", e);
        clientResponse.setStatus(CTConstants.ERROR);
        clientResponse.setMessage(e.getMessage());
    }
    return clientResponse;
}



    public static ClientResponse sendHTTPRequest(String uri, HttpEntity<ClientRequest> entity) {

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory());
        SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
        rf.setReadTimeout(CTConstants.SERVICE_TIMEOUT);
        rf.setConnectTimeout(CTConstants.SERVICE_TIMEOUT);

        ParameterizedTypeReference<ClientResponse> ptr = new ParameterizedTypeReference<ClientResponse>() {
        };
        ResponseEntity<ClientResponse> postForObject = restTemplate.exchange(uri, HttpMethod.POST, entity, ptr);
        return postForObject.getBody();

    }