2017-06-15 153 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

谢谢,现在它工作! –

相关问题