У меня есть следующая конечная точка SSE:
Код: Выделить всё
@GetMapping(path = "/api/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@PreAuthAnonymous // Subscribing to the flux is open to all, what passes through the flux is ruled by dedicated access control
public Flux sse(@RequestParam(required = false) List requiredFluxes) {
List actuallyRequiredFluxes = requiredFluxes == null ? emptyList() : requiredFluxes;
var targetJsonView = authentication().map(security::getRoles).map(IuswRole::computeViewFromUserRoles).orElse(DEFAULT_JSON_VIEW);
return Flux.merge( //
fluxes.stream().flatMap(SseFluxMethod::getFluxMethods) //
.filter(this::isAllowed) //
.filter(fluxMethod -> this.isRequiredByClient(fluxMethod, actuallyRequiredFluxes)) //
.map(SseFluxMethod::invoke) //
.toList() //
) //
.takeUntilOther(shutDown.asFlux()) //
.map(data -> SseFluxUtils.toSseEvent(data, targetJsonView));
}
Код: Выделить всё
public final class SseFluxUtils {
private SseFluxUtils() {
}
public static ServerSentEvent toSseEvent(T data) {
return toSseEvent(data, Object.class);
}
public static ServerSentEvent toSseEvent(T data, @NonNull Class jsonView) {
ObjectMapper o = JsonMapper.builder().configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true).build();
var json = o.writerWithView(jsonView).writeValueAsString(data);
return ServerSentEvent.builder().event(data.getClass().getSimpleName()).data(json).build();
}
}
Код: Выделить всё
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] SseFluxController.java:[76,21] incompatible types: inference variable V has incompatible bounds
equality constraints: org.springframework.http.codec.ServerSentEvent
lower bounds: org.springframework.http.codec.ServerSentEvent
[INFO] 1 error
[INFO] -------------------------------------------------------------
Код: Выделить всё
.map(data -> SseFluxUtils.toSseEvent(data, targetJsonView));
Спасибо за помощь!
Подробнее здесь: https://stackoverflow.com/questions/798 ... se-jackson
Мобильная версия