当前位置: 首页 > 知识库问答 >
问题:

反序列化问题

越涛
2023-03-14
public class CallSearch {

    String value;

    public CallSearch(String search){
        value = search.replace(" ", "%20");
    }
    public FullResponse search() throws ClientProtocolException, IOException{

        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet getProducts = new HttpGet("https://api.nutritionix.com/v1_1/search/"+ value +"?results=0%3A6&cal_min=0&cal_max=50000&fields=item_name%2Citem_id&appId=ac23ceb3&appKey=e2b579c55b4857157fc6045d6296b532");
        CloseableHttpResponse productResponse = client.execute(getProducts);
        String entityString = EntityUtils.toString(productResponse.getEntity());
        FullResponse test = new JSONDeserializer<FullResponse>().deserialize(entityString,FullResponse.class);

        return test;
    }
}
public class FullResponse {

    String total_hits;
    String max_score;
    List<Hits> hits;

    public FullResponse(){

    }

    public String getTotal_hits() {
        return total_hits;
    }

    public void setTotal_hits(String total_hits) {
        this.total_hits = total_hits;
    }

    public String getMax_score() {
        return max_score;
    }

    public void setMax_score(String max_score) {
        this.max_score = max_score;
    }

    public List<Hits> getHits() {
        return hits;
    }

    public void setHits(List<Hits> hits) {
        this.hits = hits;
    }
public class Hits {

       String _index;
       String _type;
       String _id;
       String _score;
       List<Fields> fields;

       public Hits(){

       }

        public String get_index() {
        return _index;
    }

    public void set_index(String _index) {
        this._index = _index;
    }

    public String get_type() {
        return _type;
    }

    public void set_type(String _type) {
        this._type = _type;
    }

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public String get_score() {
        return _score;
    }

    public void set_score(String _score) {
        this._score = _score;
    }

    public List<Fields> getFields() {
        return fields;
    }

    public void setFields(List<Fields> fields) {
        this.fields = fields;
    }
}
public class Fields {

    String item_name;

    public Fields(){

    }

    public String getField(){
        return item_name;
    }
    public void setField(String name){
        item_name=name;
    }
}

好的,我做了更改,下面是我得到的JSON响应

{
"total_hits":11025,
"max_score":11.122117,
"hits":[{
"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc210000e4",
"_score":11.122117,
"fields":{"item_name":"Whole Milk - 1 tbsp"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc2100017b",
"_score":10.7038355,
"fields":{"item_name":"2% Milk - 1 cup"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc210000f3",
"_score":10.7038355,
"fields":{"item_name":"1% Milk - 1 cup"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc210000fb",
"_score":10.689078,
"fields":{"item_name":"Skim Milk - 1 cup"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc210000e3",
"_score":10.65872,
"fields":{"item_name":"Whole Milk - 1 fl oz"}},

{"_index":"nixproductionv13",
"_type":"item",
"_id":"513fceb375b8dbbc2100017a",
"_score":10.392,
"fields":{"item_name":"2% Milk - 1 quart"}}]}

它现在导致一个嵌套异常是java.lang.IllegalArgumentException:参数类型不匹配

NestedServletException:请求处理失败;html" target="_blank">嵌套异常是java.lang.IllegalArgumentException:参数类型不匹配org.springframework.web.servlet.framework.servlet.processRequest(frameworkServlet.java:894)org.springframework.web.servlet.framework.servlet.frameworkServlet.doget(tersecurityinterceptor.invoke(filtersecurityinterceptor.java:118)org.springframework.security.web.access.incept.filtersecurityinterceptor.doFilter(filtersecurityinterceptor.java:84)org.springframework.security.wava:84)org.springframework.security.web.filterchainproxy$virtualfilterchainproxy(filterchainproxyhain.doFilter(FilterChainProxy.java:342)org.springframework.security.web.session.sessionmanagementFilter.doFilter(SessionManagementFilter.java:103)org.springframework.security.web.filterChainProxy.java:103)org.springframework.security.web.filterChainProxy.virtualFilter.doFilter(FilterChainProxy.java:342)org.springfweb.servletapi.securityContextHolderAwarerEquestFilter.doFilter(securityContextHolderAwarerEquestFilter.java:54)org.springframework.security.web.filterChainProxy$virtualFilterChain.doFilter(FilterChainProxy.java:342)org.springfilterChain.doFilter(filterChainProxy.java:342)org.springfilterChain.doFilter.security.web.w.basicauthenticationFilter.doFilter(BasicauthenticFilter.java:150)org.springframework.security.web.filterChainProxy$virtualFilterChain.doFilter(FilterChainProxy.java:342)org.springfilterChain.doFilter(FilterChainProxy.java:342)org.springframework.security.web.authentication.abstractauthEnticationProcessingFilter.doFilter(abstractingframework.security.web.filterChainProxy$virtualFilterChain.doFilter(FilterChainProxy.java:342)org.springframework.security.web.context.securityContextContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)org.springframework.security.web.filterChainProxnproxy.doFilter(filterchainproxy.java:160)org.springframework.web.filter.delegatingfilterproxy.invokeDelegate(delegatingfilterproxy.java:259)org.springfilterproxy.java

共有1个答案

曾典
2023-03-14

这不会解决一切,但会让我们走上正轨

String entityString = productResponse.getEntity().toString();

应该是

String entityString = EntityUtils.toString(productResponse.getEntity());

entityutils.tostring()javadoc状态

productResponse.getEntity().toString();
 类似资料:
  • 问题内容: 每当我尝试序列化文件时,都会收到错误消息:FileNotFound。不知道为什么。这是我的FileHelper代码: 问题答案:

  • 目前,我正在使用Avro1.8.0序列化/反序列化对象,但面临一些问题,特别是java.util.Map对象。不面临其他类型对象的问题。 这里的示例代码- 在deserialize方法中,我试图根据输入数据获取模式,但avro抛出错误- 多谢了。

  • 问题内容: 我有以下JSON片段: 全部在一个行版本中(适用于字符串文字): 在上述例子中,,,和被需要的,但是和是可选的。 这是我要反序列化的课程: 这是我用来反序列化的代码: 除此以外,一切都进行得相当顺利,并且第一次通过时都是空的。我曾尝试一切在阳光下,试图让他们填充:自定义类,,,无论是后者,和所有其他的集合,似乎适用的。[编辑:我忘记尝试了,这似乎是显而易见的候选人。没用。] 这个问题,

  • 现在我不能使用以下方法将json字符串反序列化到Object1实例: 因为Gson抛出异常: system.err:com.google.gson.jsonsyntaxException:java.lang.IllegalStateException:需要一个字符串,但BEGIN_OBJECT位于第1行column 444 path$.c位于com.google.gson.internal.bin

  • null Spring Boot 1.3.1 Jackson 2.6.4(包含JSR310模块) 这需要自定义反序列化类吗?