2017-06-15 37 views
2

我下一個端點在我的應用程序:如何在Postman中查看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()); 
} 

目前我能看到郵差文本"data:"作爲一個機構和Content-Type →text/event-stream。據我所知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

謝謝,現在它工作! –

相關問題