Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从HTTP响应返回值_Java_Json_Spring - Fatal编程技术网

在Java中从HTTP响应返回值

在Java中从HTTP响应返回值,java,json,spring,Java,Json,Spring,我在使用ResponseEntity时遇到获取值的问题。简单来说,在进行API调用并使用 ResponseEntity<String> response = *api call* String value = response.getBody(); 但是,我希望字符串:value仅等于123456,而不需要简单地强制执行它。有什么办法吗?这种行为很正常,因为您的ResponeEntity正在包装字符串 您应该使用定义属性值的类 假设您有一个定义属性值的类MyClass(使用适当的g

我在使用ResponseEntity时遇到获取值的问题。简单来说,在进行API调用并使用

ResponseEntity<String> response = *api call*
String value = response.getBody();

但是,我希望字符串:value仅等于123456,而不需要简单地强制执行它。有什么办法吗?

这种行为很正常,因为您的ResponeEntity正在包装字符串

您应该使用定义属性
值的类

假设您有一个定义属性
值的类
MyClass
(使用适当的getter),您所要做的就是按如下方式更改代码:

ResponseEntity<MyClass> response = *api call* 

String value = response.getBody().getValue();
ResponseEntity response=*api调用*
字符串值=response.getBody().getValue();

不要使用字符串作为
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();
然后像这样使用这个对象:

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();
ResponseEntity response=*api调用*
字符串值=response.getBody().getValue();

不要要求回复。请求答复。如果你发布了你的代码,解释如何修改它会容易得多。我建议使用这种方法