首页 > 解决方案 > spring Rest APIcall 在本地工作正常但在生产中失败

问题描述

我有一个休息端点http://abc-xyz.doamin.com.au:8443/myapplication/v2/test我在我的程序中使用它,它在本地运行良好但在生产中失败

例外

"message":"HTTP exception while calling TnDM",
"logger_name":"com.myapp.resource.MyResource",
"thread_name":"http-nio-8080-exec-7",
"level":"ERROR",
"level_value":40000,
"stack_trace":"org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error\n\t
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:89)\n\t
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:708)\n\t
at

本地工作正常,200 OK

{
    "text": "Hello_Message"
}

代码

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HelloWorldRequest req = new HelloWorldRequest();
    request.setName("myName");

    HttpEntity entity = new HttpEntity(req, headers);
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

标签: spring-bootmicroservicesspring-rest

解决方案


创建一个 pojo 来处理您上面提到的响应类型:

public class Response
 {
    private String text;
    //getter, setter
 }

然后更改此行:

ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

有了这个:

ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.POST, entity, Response.class);

希望,这会奏效!


推荐阅读