json - 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);
}
}
解决方案
推荐阅读
- java - 错误 org.hibernate.util.JDBCExceptionReporter - ORA-02291:违反完整性约束 (PERFMGMT.ZAUTOMATION_PHASE_FK1) - 未找到父键
- ios - 为 iOS 构建,但在为 iOS 模拟器构建的 dylib 文件中链接
- python - 检查是否至少一列包含熊猫中的字符串
- python - 有没有办法将 SQL 表中的文本与 case 语句结合起来
- r - CRAN 提交中包含的文件和目录
- javascript - 如何从 CKEditor 5 捕获外部事件?
- android - 如何插入 FFI 功能的完成百分比?
- javascript - 如何添加linkedin个人资料徽章以响应应用程序
- reactjs - Cypress open-ct 和 run-ct 破坏了我的 CPU
- dart - 为什么在检查可空性的 if 语句的范围内仍然可以将可空值视为可空值?