我在使用 ResponseEntity 时获取值时遇到问题。简而言之,在进行 API 调用并使用之后
ResponseEntity<String> response = *api call*
String value = response.getBody();
我得到输出
{"value":123456}
但是我希望 string: value 等于 123456,而不是简单地强制它。关于如何做到这一点有什么想法吗?
最佳答案
不要使用字符串作为 ResponseEntity
对象的泛型。
像这样创建您自己的对象:
public class MyResponse {
private String value;
public String getValue () {
return this.value;
}
public void setValue (String value) {
this.value = value;
}
}
然后像这样使用这个对象:
ResponseEntity<MyResponse> response = *api call*
String value = response.getBody().getValue();
关于java - Java 中 HTTP 响应的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52820695/