【问题标题】:RestTemplate consuming Json stringRestTemplate 使用 Json 字符串
【发布时间】:2018-08-06 09:56:10
【问题描述】:

我正在尝试使用 Spring RestTemplate 使用返回内容类型为 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)

结果总是包含"\"a string with double quotes, Json valid\"",而我想接收它转义,"a string with double quotes, Json valid"

我也希望无需解析即可收到此答案。

对我来说这似乎是一个微不足道的问题,但我没有在网上找到任何 SO 问题或其他资源。

【问题讨论】:

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

标签: java resttemplate


【解决方案1】:

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

所以,要进行反序列化,我可以使用两种方式:

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

    @AllArgsConstructor
    @NoArgsConstructor
    public class StringHolder {
    
      @JsonIgnore
      private String value;
    
      @JsonCreator
      public static StringHolder create(String value){
        return new StringHolder(value);
      }
    }
    

    现在,template.postForObject("http://...", request, StringHolder.class).getValue() 正在返回转义的字符串。

【讨论】:

    【解决方案2】:

    就我而言,restfull 返回了一个 JSON 矩阵,所以我找到了一个将 JSON 转换为 pojos 的页面,这就是我返回的内容

    http://pojo.sodhanalibrary.com/

    // MyPojo
    
    public class MyPojo
    {
        private Items[] items;
    
        private First first;
    
        public Items[] getItems ()
        {
            return items;
        }
    
        public void setItems (Items[] items)
        {
            this.items = items;
        }
    
        public First getFirst ()
        {
            return first;
        }
    
        public void setFirst (First first)
        {
            this.first = first;
        }
    
        @Override
        public String toString()
        {
            return "ClassPojo [items = "+items+", first = "+first+"]";
        }
    }
    
    // CLASE DE FIRST
    
    public class First
    {
        private String $ref;
    
        public String get$ref ()
        {
            return $ref;
        }
    
        public void set$ref (String $ref)
        {
            this.$ref = $ref;
        }
    
        @Override
        public String toString()
        {
            return "ClassPojo [$ref = "+$ref+"]";
        }
    }
    
    
    // CLASE ITEM
    
    public class Items
    {
        private String nombre_inmueble;
    
        private String inmueble_id;
    
        public String getNombre_inmueble ()
        {
            return nombre_inmueble;
        }
    
        public void setNombre_inmueble (String nombre_inmueble)
        {
            this.nombre_inmueble = nombre_inmueble;
        }
    
        public String getInmueble_id ()
        {
            return inmueble_id;
        }
    
        public void setInmueble_id (String inmueble_id)
        {
            this.inmueble_id = inmueble_id;
        }
    
        @Override
        public String toString()
        {
            return "ClassPojo [nombre_inmueble = "+nombre_inmueble+", inmueble_id = "+inmueble_id+"]";
        }
    }
    
    
    // CLASE PRINCIPAL
    
    @SpringBootApplication
    public class ApeplazasDropDownApplication {
    
    	private static final Logger log = LoggerFactory.getLogger(ApeplazasDropDownApplication.class);
    	
    	
    	public static void main(String[] args) {
    		
    		final String urlGETList = "http://localhost/test/getAll";
    		RestTemplate restTemplate = new RestTemplate();
    		
    		
    		MyPojo quote = restTemplate.getForObject(urlGETList, MyPojo.class);
    	    log.info(quote.getItems()[1].getInmueble_id());
    	
    		
            	
            }
            
    		
    		
    	}
    
    // MY API
    
    {
      
      "items": [
     
                 {
     "inmueble_id": 48,
    
                   "nombre_inmueble": "ACAPULCO"
     
                 },
     
                 {
     "inmueble_id": 33,
    
                 "nombre_inmueble": "MONTERREY"
    
                 },
    	  ],
       "first": 
              {
    
    	  "$ref": "https://localhost/test/getAll"
    
              }
    
    }

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 2018-08-07
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多