simplyi / SpringCloudVideoCourse

121 stars 295 forks source link

restTemplate.exchange doesn't work #12

Open hazartilirot opened 1 year ago

hazartilirot commented 1 year ago

Sergey,

I believe I need a hand with the lesson. I'm watching "Make Users Microservice call Albums Microservice" at the moment. The snippet of code is exactly what bothers me at the moment:

 /*
        String albumsUrl = String.format(environment.getProperty("albums.url"), userId);

        ResponseEntity<List<AlbumResponseModel>> albumsListResponse = restTemplate.exchange(albumsUrl, HttpMethod.GET, null, new ParameterizedTypeReference<List<AlbumResponseModel>>() {
        });
        List<AlbumResponseModel> albumsList = albumsListResponse.getBody(); 
        */

In the video it works perfectly fine however I went to the most recent version of your repository

to my surprise the code has been commented out. Wouldn't you mind explaining me what arguments I should provide to the restTemplate.exchange ?

Thank you in advanced.

simplyi commented 1 year ago

The code was commented out because I started using Feign client. But if you do not use Feign then, removing comments should make RestTemplate work. Same parameters as I have in my code.

Have you created RestTemplate bean? 🤔I create an instance of RestTemplate in the main application file. The one that has public static void main().

On Sat, Apr 8, 2023 at 7:41 PM hazartilirot @.***> wrote:

Sergey,

I believe I need a hand with the lesson. I'm watching "Make Users Microservice call Albums Microservice" at the moment. The snippet of code is exactly what bothers me at the moment:

/* String albumsUrl = String.format(environment.getProperty("albums.url"), userId);

    ResponseEntity<List<AlbumResponseModel>> albumsListResponse = restTemplate.exchange(albumsUrl, HttpMethod.GET, null, new ParameterizedTypeReference<List<AlbumResponseModel>>() {
    });
    List<AlbumResponseModel> albumsList = albumsListResponse.getBody();
    */

In the video it works perfectly fine however I went to the most recent version https://github.com/simplyi/SpringCloudVideoCourse/blob/update-spring-boot-v3.0.1/PhotoAppApiUsers/src/main/java/com/appsdeveloperblog/photoapp/api/users/service/UsersServiceImpl.java of your repository

to my surprise the code has been commented out. Wouldn't you mind explaining me what arguments I should provide to the restTemplate.exchange ?

Thank you in advanced.

— Reply to this email directly, view it on GitHub https://github.com/simplyi/SpringCloudVideoCourse/issues/12, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFIU5B2LMY4WCWEQT2I5DTXAHZUPANCNFSM6AAAAAAWXXUEL4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

hazartilirot commented 1 year ago

Affirmative! I have not reached the Feign part yet.

I usually spend a couple of hours checking out the whole process over again trying to figure out how to solve the issue on my own. I think it doesn't like the amount of arguments I pass:

Screenshot 2023-04-09 at 09 41 00

I went to the documentation

As far as I understand the only overloaded method we can use with four arguments should consists of (what I took in a blue rectangle):

Screenshot 2023-04-09 at 09 52 01

Therefore I also tried to wrap the link in URI.create(albumsUrl) to convert String to URI, but it doesn't work either.

I have compressed the whole project. If you have time, could you check it out? workspace_microservices.zip

Mind, I updated Java to 20th yesterday so I had to change SDK in the main project to 20th. The IDE I use is Intellij IDEA 2023.1.

hazartilirot commented 1 year ago

Finally I have found an example on the Internet which helped me to solve the issue with it. I'm still interested in the issue preventing the method from running although the solution I did is:

        String albumsUrl = String.format(Objects.requireNonNull(env.getProperty("albums.url")), userId);

        var albumsListResponse = restTemplate.getForEntity(albumsUrl, ReadAlbumResponseModel[].class);

        var albumsList = List.of(Objects.requireNonNull(albumsListResponse.getBody()));

        userDto.setAlbums(albumsList);

        return userDto;

there would then be another issue in the project running on Spring Cloud Gateway (Not Zuul). Open WebSecurity.java

and after the line .requestMatchers(HttpMethod.GET, "/users/status/check").permitAll() paste the line .requestMatchers(HttpMethod.GET, "/users/**").permitAll() Otherwise you will always get 403 error (Forbidden).