2017-06-15 3 views
2

私は自分のアプリケーション内の次のエンドポイントがあります。郵便配達でのSpring 5 Reactive APIからの返答を表示するには?

@GetMapping(value = "/users") 
public Mono<ServerResponse> users() { 
    Flux<User> flux = Flux.just(new User("id")); 
    return ServerResponse.ok() 
      .contentType(APPLICATION_JSON) 
      .body(flux, User.class) 
      .onErrorResume(CustomException.class, e -> ServerResponse.notFound().build()); 
} 

現在、私は郵便配達で、本体とContent-Type →text/event-streamとしてテキスト"data:"を見ることができます。私が理解しているようにMono<ServerResponse>は常にSSE(Server Sent Event)でデータを返します。 Postmanクライアントで何とか応答を表示できますか?

+0

こんにちは、あなたは郵便配達よりも、別のコンシューマを使用して応答を得ることを確認していますか? – Seb

+0

@Seb実際、私はコードを使いこなしていました。ドキュメントでこの例を見ました。 –

+0

'Mono 'は必ずしもSSEとしてデータを返すわけではありません。 WebFluxのバグか、設定に何か間違っています。最新のマイルストーンでもう一度試してみるか、サンプルプロジェクトでjira.spring.ioの問題を開くことはできますか? –

答えて

1

WebFluxの注釈モデルと機能モデルが混在しているようです。 ServerResponseクラスは、機能モデルの一部です。

はここWebFluxに注釈付きエンドポイントを記述する方法は次のとおりです。

@RestController 
public class HomeController { 

    @GetMapping("/test") 
    public ResponseEntity serverResponseMono() { 
     return ResponseEntity 
       .ok() 
       .contentType(MediaType.APPLICATION_JSON) 
       .body(Flux.just("test")); 
    } 
} 

ここで機能的な方法は、今だ:

@Component 
public class UserHandler { 

    public Mono<ServerResponse> findUser(ServerRequest request) { 
     Flux<User> flux = Flux.just(new User("id")); 
     return ServerResponse.ok() 
       .contentType(MediaType.APPLICATION_JSON) 
       .body(flux, User.class) 
       .onErrorResume(CustomException.class, e -> ServerResponse.notFound().build()); 
    } 
} 

@SpringBootApplication 
public class DemoApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(DemoApplication.class, args); 
    } 


    @Bean 
    public RouterFunction<ServerResponse> users(UserHandler userHandler) { 
     return route(GET("/test") 
        .and(accept(MediaType.APPLICATION_JSON)), userHandler::findUser); 
    } 

} 
+0

ありがとうございました、今は動作します! –

関連する問題