Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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 使用Json字符串的RestTemplate_Java_Resttemplate - Fatal编程技术网

Java 使用Json字符串的RestTemplate

Java 使用Json字符串的RestTemplate,java,resttemplate,Java,Resttemplate,我试图使用SpringRESTTemplate使用一个返回内容类型为application/json的json字符串的服务 响应如下所示: ============================response begin========================================== response: 200 OK Headers Content-Length : 108 Content-Type : application/json; char

我试图使用SpringRESTTemplate使用一个返回内容类型为
application/json
的json字符串的服务

响应如下所示:

============================response begin==========================================
response: 200 OK
Headers
    Content-Length  : 108
    Content-Type    : application/json; charset=utf-8
Response Body : "a string with double quotes, JSON valid"
=======================response end=================================================
现在,当我使用:

String result = template.postForObject("http://...", request, String.class)
结果总是包含一个带双引号的字符串,Json有效\,而我希望接收它转义,
“一个带双引号的字符串,Json有效”

我也希望不必解析就能收到这个答案


对我来说,这似乎是一个很小的问题,但我在web上没有发现任何这样的问题或其他资源。

似乎指定了
String。class
会将整个主体作为字符串提供给您,而不会进行任何形式的反序列化(即使使用了
application/json
,即使字符串实际上是一种有效的json形式)。这是因为默认情况下会将
StringHttpMessageConverter
添加到
RestTemplate
转换器中

因此,要进行反序列化,我可以使用两种方法:

  • 从RestTemplate的转换器中删除
    StringHttpMessageConverter
  • 使用具有正确
    @JsonCreator
    方法的虚拟“holder”类

    @AllArgsConstructor
    @NoArgsConstructor
    public class StringHolder {
    
      @JsonIgnore
      private String value;
    
      @JsonCreator
      public static StringHolder create(String value){
        return new StringHolder(value);
      }
    }
    
    现在,
    template.postForObject(“http://...,请求,StringHolder.class)。getValue()将返回转义字符串


在我的例子中,restfull返回了一个JSON矩阵,因此我找到了一个将JSON转换为POJO的页面,这就是我返回的内容

//MyPojo
公共类MyPojo
{
私人物品[]物品;
私人优先;
公共项目[]获取项目()
{
退货项目;
}
公共无效集合项(项[]项)
{
这个项目=项目;
}
公共优先获取优先()
{
先返回;
}
公共无效设置优先(优先)
{
this.first=first;
}
@凌驾
公共字符串toString()
{
返回“ClassPojo[items=“+items+”,first=“+first+”];
}
}
//第一类
头等舱
{
私有字符串$ref;
公共字符串获取$ref()
{
返回$ref;
}
公共无效集$ref(字符串$ref)
{
这.$ref=$ref;
}
@凌驾
公共字符串toString()
{
返回“ClassPojo[$ref=“+$ref+”]”;
}
}
//CLASE项目
公共类项目
{
私有字符串名为inmueble;
私有字符串inmueble_id;
公共字符串getNombre_inmueble()
{
返回名称为inmueble;
}
public void setNombre_inmueble(字符串nombre_inmueble)
{
this.nombre_inmueble=nombre_inmueble;
}
公共字符串getInmueble_id()
{
返回MUEBLE_id;
}
公共无效设置inmueble\u id(字符串inmueble\u id)
{
this.inmueble_id=inmueble_id;
}
@凌驾
公共字符串toString()
{
返回“ClassPojo[nombre_inmueble=“+nombre_inmueble+”,inmueble_id=“+inmueble_id+””;
}
}
//克拉斯校长
@SpringBoot应用程序
公共类ApeplazasDropDownApplication{
私有静态最终记录器log=LoggerFactory.getLogger(apeplazasdroptdownplication.class);
公共静态void main(字符串[]args){
最后一个字符串urlGETList=”http://localhost/test/getAll";
RestTemplate RestTemplate=新RestTemplate();
MyPojo quote=restemplate.getForObject(urlGETList,MyPojo.class);
log.info(quote.getItems()[1].getInmueble_id());
}
}
//我的API
{
“项目”:[
{
“inmueble_id”:48,
“nombre_inmueble”:“阿卡普尔科”
},
{
“inmueble_id”:33,
“nombre_inmueble”:“蒙特雷”
},
],
“第一”:
{
“$ref”:”https://localhost/test/getAll"
}

}
您确定这里的服务响应没有问题吗?我一开始就是这么想的,基本上我收到的是带双引号的“字符串”。但它最终是有效的。不过,这值得研究一下,我来看看。你的API是否返回了一个json,但用双引号包装,本质上只是一个字符串。是的,就是这样。