我想在对Springboot应用程序的请求中发送一个表示JSON的字符串。我知道,如果我们发送JSON请求,并创建JSON请求等效对象模型,那么Springboot会自动将传入的JSON请求映射到Java对象。
一旦我的springboot应用程序接收到字符串请求,我就可以将字符串转换为JSON,然后使用Jackson将其转换为对象模型,但我不想这样做,我需要一个类似于JSON的映射,默认的Springboot映射是对象转换。
需要帮助才能实现同样的效果。
下面是我的控制器:
@PostMapping(value = "/eventListener/v5",
consumes = MediaType.APPLICATION_JSON_VALUE)
public String receiveEventForV5(@RequestBody EventV5 event){
logger.info(" FaultEvent Details : " + event);
eventValidationService.validateEvent(VES5_VERSION,event);
return "FaultEvent received" + event;
}请求字符串为:
“事件”:{
"commonEventHeader": {
"version": 2.0,
"eventName": "abc",
"domain": "fault1",
"eventId": "001-000000000001",
"eventType": "adadas",
"nfcNamingCode": "ijk",
"nfNamingCode": "add",
"sourceId": "source_entity_id_123abc",
"sourceName": "source_entity_name_oam",
"reportingEntityId": "reporting_entity_id_123abc",
"reportingEntityName": "reporting_entity_name_oam",
"priority": "High",
"startEpochMicrosec": 1527089079615,
"lastEpochMicrosec": 1527089079615,
"sequence": 0
},它仍然显示不支持的数据类型。
发布于 2019-01-14 12:54:54
解决方案1:
您可以使用spring mvc中的ObjectMapper使用HandlerMethodArgumentResolver将字符串体转换为对象。
例如,如何使用HandlerMethodArgumentResolver:https://github.com/jinternals/custom-argument-resolvers-spring-controller
解决方案2:
尝试使用AbstractHttpMessageConverter自定义http消息转换器。
https://stackoverflow.com/questions/54175900
复制相似问题