开发者

Unable to save data received by Spring Boot WebCliet

开发者 https://www.devze.com 2022-12-07 17:22 出处:网络
The things I want to do is: to get data from开发者_JAVA技巧 https://jsonplaceholder.typicode.com/ and save those data into my machine. I want to save the posts from this site. I want to do it by Sprin

The things I want to do is: to get data from开发者_JAVA技巧 https://jsonplaceholder.typicode.com/ and save those data into my machine. I want to save the posts from this site. I want to do it by Spring Boot WebClient. I have followed several tutorials, articles, and also WebClient documentation. But Unable to save the response in my local database.

The below URL will return one post. https://jsonplaceholder.typicode.com/posts/1

If I want to return the post as the response of another API it is working fine, but not able to use the inside program. I have tried with WebClient .block(), but it is working for standalone applications but not for web application.

GitLab link of the project

Controller :

    @Autowired
    private PostService postService;
    
    // working fine.
    @GetMapping(value = "posts", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public Flux<Post> findAll() {
        return postService.findAll();
    }

    @GetMapping(value = "postsSave")
    @ResponseStatus(HttpStatus.OK)
    public String saveAll() {
        return postService.saveAll();
    }

Service:

    @Override
    public String saveAll() {
//        Post posts = webClient.get()
//                .uri("/posts")
//                .retrieve()
//                .bodyToFlux(Post.class)
//                .timeout(Duration.ofMillis(10_000)).blockFirst();

        String url = "https://jsonplaceholder.typicode.com/posts/1";
        WebClient.Builder builder = WebClient.builder();
        Post p = builder.build()
                .get()
                .uri(url)
                .retrieve()
                .bodyToMono(Post.class)
                .block(); // this line generating error.

        postRepository.save(p);
        return "saved";
    }

Exception StackTrace:

2022-12-07 14:35:44.070 ERROR 6576 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [b48b7f19-1]  500 Server Error for HTTP GET "/postsSave"

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-3
    at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83) ~[reactor-core-3.3.9.RELEASE.jar:3.3.9.RELEASE]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ HTTP GET "/postsSave" [ExceptionHandlingWebHandler]
Stack trace:
        at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83) ~[reactor-core-3.3.9.RELEASE.jar:3.3.9.RELEASE]
        at reactor.core.publisher.Mono.block(Mono.java:1680) ~[reactor-core-3.3.9.RELEASE.jar:3.3.9.RELEASE]
        at com.quantsys.service.PostService.saveAll(PostService.java:53) ~[classes/:na]
        at com.quantsys.controller.PostController.saveAll(PostController.java:26) ~[classes/:an]

But the same code of snippet is working within the Bootstrap class:

@SpringBootApplication
public class QuanrsysPostService {
    public static void main(String[] args) {
        SpringApplication.run(QuanrsysPostService.class, args);

        String url = "https://jsonplaceholder.typicode.com/posts/1";
        WebClient.Builder builder = WebClient.builder();
        Post p = builder.build()
                .get()
                .uri(url)
                .retrieve()
                .bodyToMono(Post.class)
                .block(); // working here.
        System.out.println(p.toString());
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号