restTemplate.postForEntity(url,entity, String.class);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
尝试创建对象时抛出空指针异常。
我检查了实体和 url,它被打印在 Logger 消息中。但是在这一行它抛出空指针异常,但对象仍在创建中..
如果对象正在创建,这怎么会抛出空指针异常..
我正在使用 kubernetes,当我在 kubernetes 中检查命令行时它说对象已创建..但在日志中它显示空指针异常
最佳答案
问题似乎是您正在执行请求两次。
restTemplate.postForEntity(url, entity, String.class);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
postForEntity
和 exchange
都在向您的 url
发送发布请求。 postForEntity
可以看作是 exchange
方法的一个特例。查看documentation
请只使用其中一个,例如:
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
关于java - resttemplate.exchange 抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45076189/