【问题标题】:How to get webflux endpoint to serialize json with object name for each item如何让 webflux 端点用每个项目的对象名称序列化 json
【发布时间】:2021-10-09 13:53:29
【问题描述】:

我有一个返回项目数组的 webflux 端点。我试图让它将对象名称添加到@JsonRootName 指定的数组中的每个项目中。我试过像这样使用 Jackson2ObjectMaper 配置对象映射器。我正在使用 SpringBoot 2.4.4。

@Configuration
@EnableWebFlux
public class JacksonConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer() {
        return builder -> {
            builder.modules(new JavaTimeModule());
            builder.featuresToEnable(SerializationFeature.WRAP_ROOT_VALUE);
            builder.featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            builder.indentOutput(true);
        };
    }
}

我的端点如下:

@GetMapping(value = "/all/{id}", produces = "application/json")
public ResponseEntity<Flux<EventsMetaData>> getAllEventsByUser(@PathVariable String id) {
    return ResponseEntity.ok(eventsMetaDataService.getAllEventsByUserId(id));
}

Json 响应是:

[
    {
        "id": "342424234",
        "userid": "34324242",
        "eventId": null,
        "eventName": "Test 1",
        "eventDetails": null
    },
    {
        "id": "43435235",
        "userid": "34244242",
        "eventId": null,
        "eventName": "Test",
        "eventDetails": null
    }
]

预期输出:

[
   Event: {
        "id": "342424234",
        "userid": "34324242",
        "eventId": null,
        "eventName": "Test 1",
        "eventDetails": null
    },
    Event: {
        "id": "43435235",
        "userid": "34244242",
        "eventId": null,
        "eventName": "Test",
        "eventDetails": null
    }
]

但是,当我的对象使用 @JsonRootName 注释时,我没有得到根对象

@JsonRootName(value = "Event")
public class Event {

欢迎提出任何建议。

编辑: 从配置中删除 @EnableWebFlux 后,它给了我一个根包装值,但仍然不像它应该使用列表那样:

{
    "List": [
        {
            "id": "610bd71077cfcd2ee941217c",
            "eventName": "Test",
            "eventDetails": null,
            "eventDateTime": null,
            "eventLink": null
        }
    ]
}

然而,单个对象会像这样尊重包装器:

{
    "event": {
        "id": "610bd71077cfcd2ee941217c",
        "eventName": "Test",
        "eventDetails": null,
        "eventDateTime": null,
        "eventLink": null
    }
}

链接到复制项目https://gitlab.com/dmbeer/so-68651608 即使json结构错误,测试列表也不应该存在。

【问题讨论】:

    标签: json spring-boot jackson spring-webflux


    【解决方案1】:

    我认为你需要像这样配置Bean

    @Configuration
    public class ApplicaitonConfiguration {
    
        @Bean
        ObjectMapper objectMapper() {
    
            var mapper = JsonMapper.builder().addModules(new JavaTimeModule()).build();
    
            // Enable your features...
    
            return mapper;
        }
    }
    

    【讨论】:

    • 试过这个,但没有运气仍然是相同的输出。似乎 RestController 没有使用配置的 ObjectMapper
    • 它不像你期望的那样工作,因为你想要的甚至不是一个有效的 JSON。
    • 请参阅已编辑的问题,包装器不应显示列表,而是将事件作为单个项目显示。以及为什么当我删除 @EnableWebFlux 时它会起作用
    • 因为我看不到你的整个代码库,所以很难说为什么删除 EnableWebFlux 有效。至于 JSON,您的预期输出甚至不是有效的 JSON。
    • 我添加了一个指向具有相同问题的复制项目的链接。即使 json 结构错误,文本列表也不应该存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2018-03-24
    相关资源
    最近更新 更多