首页 > 解决方案 > Json 解析错误。无法从对象值反序列化

问题描述

出现意外错误(类型=错误请求,状态=400)。

JSON解析错误:

Can not construct instance of javax.xml.bind.JAXBElement: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); 

nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of javax.xml.bind.JAXBElement: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: java.io.PushbackInputStream@da18a91; line: 1, column: 622] (through reference chain:

下面是我的代码:这是我的控制器

@RestController
@RequestMapping(value = { "/api" })
public class TestController {

    /*
     * @Autowired BeanTest bean;
     */

    private final static QName _PersonalNameTypeGivenNames_QNAME = new QName("http://sita.aero/iborders/evisa/Common/V1", "GivenNames");
    private final static QName _PersonalNameTypeFamilyName_QNAME = new QName("http://sita.aero/iborders/evisa/Common/V1", "FamilyName");

    @RequestMapping(value="/getBeanTest", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> getTestBean() {
        BeanTest bean = new BeanTest();
        List<BeanTest> beanTest = new ArrayList<BeanTest>();
        final List<JAXBElement<String>> jaxbElements = new ArrayList<JAXBElement<String>>();
        JAXBElement<String> jaxbElementGivenNames = new JAXBElement<String>(_PersonalNameTypeGivenNames_QNAME, String.class, BeanTest.class, "Gaurav");
        jaxbElements.add(jaxbElementGivenNames);
        JAXBElement<String> jaxbElementFamilyNames = new JAXBElement<String>(_PersonalNameTypeFamilyName_QNAME, String.class, BeanTest.class, "Chhimwal");
        jaxbElements.add(jaxbElementFamilyNames);
        bean.getContent().addAll(jaxbElements);

        return new ResponseEntity<Object>(bean,HttpStatus.OK);
    }

}

我的实体是

public class BeanTest {

    protected List<JAXBElement<String>> content;


    public List<JAXBElement<String>> getContent() {
        if (content == null) {
            content = new ArrayList<JAXBElement<String>>();
        }
        return this.content;
    }

}

我正在从另一个休息 api 调用此服务。调用服务时出现此错误:

@RestController
@RequestMapping(value = { "/api" })
public class SpringRestController {

    @Autowired
    private RestTemplateBuilder restTemplateBuilder;

    @Autowired
    public Environment env;



    @GetMapping(value = "/getApplicationTest", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> getApplicationTest() {

        RestTemplate restTemplate = restTemplateBuilder.build();

        BeanTest obj = restTemplate.getForObject("http://localhost:1010/api/getBeanTest",
                BeanTest.class);
        return new ResponseEntity<Object>(obj, HttpStatus.OK);
    }
}

标签: jsonrestspring-boot

解决方案


推荐阅读