当前位置: 首页 > 知识库问答 >
问题:

WebClient调用中的Webflux微服务错误;

汪驰
2023-03-14

我试图通过使用Java+Spring+WebFlux开始反应性编程。

@RestController
@RequestMapping("/customers")
public class CustomerController {

@GetMapping("/{id}")
    public Mono<Customer> customerById(@PathVariable String id){
        return customerService.findById(id);
    }
}

public class Customer {

    public Customer(String id, String fullName) {
        this.id = id;
        this.fullName = fullName;
    }

    @Id
    private String id;
    @NotNull(message = "The name must not be null")
    private String fullName;
    private String email;
    private String document;
}
//Service Class
public Mono<Customer> findById(String id){
        return Mono.just(new Customer(id, "Joseph"));
    }

//Method on Controller
@RestController
@RequestMapping("v1/customers")
public class CustomerController {

@GetMapping("/{id}")
    public Mono<Customer> customerById(@PathVariable String id){
        return customerService.findById(id);
    }
}
{
id: "123",
fullName: "Joseph",
email: null,
document: null
}

当我在customer consumerendpoint上调用get时,我得到一个httpcode=500

下面是客户消费者上的服务层

    private String BASE_URL = "http://localhost:8060/";

    public Mono<Customer> findById(String id){
        WebClient.Builder builder = WebClient.builder();
       return builder               
                .baseUrl(BASE_URL)
                .build()
                .get()
                .uri("customers/{id}", id)
                .retrieve()
                .bodyToMono(Customer.class);
    }

我还尝试删除.baseurl(BASE_URL).uri()中的concinting,但没有成功。

2019-12-01 22:21:45.910 ERROR 16788 --- [ctor-http-nio-2] a.w.r.e.AbstractErrorWebExceptionHandler : [1510eee8]  500 Server Error for HTTP GET "/v1/customers/123"

java.lang.NullPointerException: null at com.poc.controller.CustomerController.findById(CustomerController.java:19) ~[classes/:na]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ? HTTP GET "/v1/customers/123" [ExceptionHandlingWebHandler]
Stack trace:
        at com.poc.webportal.controller.CustomerController.findById(CustomerController.java:19) ~[classes/:na]

共有1个答案

松铭
2023-03-14

这很尴尬,但我忘了在控制器上注入CustomerService

所以,autowired解决了这个问题。

 类似资料:
  • 这似乎是我想做的:Spring Webflux:Webclient:让body出现错误,但仍然不能让它工作。不确定exchange()是什么

  • 如果类路径上有SpringWebFlux,还可以选择使用WebClient调用远程REST服务。与RestTemplate相比,这个客户端具有更多的功能感和完全的反应性。您可以在SpringFrameworkdocs中的专用部分中了解更多关于WebClient的信息。 Spring Boot为您创建并预配置WebClient.Builder; 强烈建议将其注入组件并使用它来创建WebClient实

  • 我有一种情况,我可以使用阻塞I/O库访问外部服务上的RESTendpoint,或者在这种情况下,我可以直接使用HttpClient(如WebClient)调用REST。现在,我想知道在包装对该库的调用和将其发布到弹性线程或使用WebClient访问endpoint之间是否存在性能差异。 如何准确地处理调用这两个选项。所以我们假设网络流量使用单线程来处理请求。然后,请求将由WebClient处理。这

  • 我正在使用尤里卡发现服务器。我可以通过通过使用服务名从另一个微服务调用一个微服务,这对Eureka服务器是可见的。我听说在不久的将来将被弃用。 我还可以通过使用从另一个微服务调用一个微服务,但在本例中,我需要使用整个主机名。我不能叫他们服务名称。 是的,我可以从属性文件设置基本url,这样不需要重新启动jar,我们就可以更改基本url。如果我们这样做,那么我们可能不需要任何discovery服务器

  • 我想在以下条件下抛出自定义异常: > 如果我得到了json格式的正确错误响应,我想反序列化它,并在onStatus()中抛出名为CommonException的异常 如果作为响应的一部分接收HTML内容或反序列化未成功执行,则我希望抛出GenericeException,这是我在onErrorMap()中创建的 在抛出GenericException时,我希望将从下游响应获得的相同HttpStat

  • 我试图使用WebClient实现以下场景。使用RestTemplate很琐碎,但我再也做不到了。