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

Jackson:无法反序列化START_OBJECT令牌外的`java.lang.string`实例

姬慎之
2023-03-14

我编写了一个微服务来对API进行HTTP调用。代码如下所示。
连接器应用程序

package com.ajay.dashboard.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class DellDashboardConnectorApplication {

    public static void main(String[] args) {
        SpringApplication.run(DellDashboardConnectorApplication.class, args);
    }

    @Bean
    public RestTemplate getRestTemplate() {

        return new RestTemplate();
    }
}  

连接器控制器

package com.ajay.dashboard.service.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/*
 * Created by Kulkaa
 */

@RestController
public class DellDashboardController {

    private static final Logger logger = LoggerFactory.getLogger(DellDashboardController.class);

    @CrossOrigin(origins = "http://localhost:8080")
    @RequestMapping(method = RequestMethod.GET, value = "/incident", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> retrieveAllCircles(HttpServletRequest request) throws UnsupportedEncodingException {
        logger.info("DellDashboardController -> retrieveAllIncidents : invoked.");
        RestTemplate restTemplate =new RestTemplate();

        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); 
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
        messageConverters.add(converter);
        restTemplate.setMessageConverters(messageConverters);


        String formUrl = "api";
        final String sysparm_query = "incident_stateNOT%20IN6%2C7%5Eassignment_group%3D4122c7f8f09cc1002283ac3a043ae3e6";
        final String sysparm_display_value = "true";
        final String sysparm_exclude_reference_link = "true";
        try {
                URIBuilder builder = new URIBuilder(formUrl); 
                builder.addParameter("sysparm_query", sysparm_query); 
                builder.addParameter("sysparm_display_value", sysparm_display_value);
                builder.addParameter("sysparm_exclude_reference_link", sysparm_exclude_reference_link); 
                String actualUrl = builder.toString();
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "Basic U2VydmljZV9Nb2JpbGVSZXBvcnRpbmc6U2VydmljZV9Nb2JpbGVSZXBvcnRpbmc=");
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            HttpEntity<String> entity = new HttpEntity<String>(headers);
            return restTemplate.exchange(actualUrl, HttpMethod.GET, entity, String.class);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return retrieveAllCircles(request);

    }

}  

当我使用mvn clean install构建它时,它运行得非常完美。然而,当我将其作为SpringBoot应用程序运行时,我会得到以下错误:

{
    "result": [{
        data here
    }]
}  

共有1个答案

宰父涵忍
2023-03-14

尝试创建空实体。这可能行得通

 HttpEntity<String> entity = new HttpEntity<String>(null,headers); 

JSON内容无效,因此解析器中断。因此这将导致内容为空。

 类似资料: