spring-boot - 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);
解决方案
创建一个 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);
希望,这会奏效!
推荐阅读
- fullcalendar - 在午夜开始的单元格中显示事件(夜班)
- python - 关于 numpy.random.choice 的工作原理
- sql - Oracle (SQL):如何获取 SQL 查询语法
- verilog - 使用三元运算符 (Verilog) 的意外行为
- php - PHP _Session 变量在 php 文件之间丢失。为什么?
- scala - Scala App trait 和 main 如何在内部工作?
- c# - 线程无缘无故死在特定行
- python - mysqlclient 无法在虚拟环境中安装
- reactjs - 在 Amazon MTurk 的 HTMLQuestion 中使用 Vue 或 React
- scala - Scala Future 究竟返回了什么?