Skip to content

Commit

Permalink
refactor main class
Browse files Browse the repository at this point in the history
  • Loading branch information
huyvu8051 committed Apr 21, 2024
1 parent 7579f08 commit b632166
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,7 @@

@SpringBootApplication
public class HichaBusinessApplication {

public static void main(String[] args) {
SpringApplication.run(HichaBusinessApplication.class, args);
}

@Bean
JsonPlaceholderService jsonPlaceholderService() {
RestClient restClient = RestClient.create("https://jsonplaceholder.typicode.com");
HttpServiceProxyFactory fact = HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient)).build();
return fact.createClient(JsonPlaceholderService.class);
}

@Bean

@Observed(name = "posts.load-all-posts", contextualName = "post.find-all")
CommandLineRunner commandLineRunner(JsonPlaceholderService jsonPlaceholderService) {
return args -> {
jsonPlaceholderService.findAll();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.huyvu.hicha.hichabusiness.config;

import io.huyvu.hicha.hichabusiness.service.JsonPlaceholderService;
import io.micrometer.observation.annotation.Observed;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class InitialCommandConfiguration {
@Bean
@Observed(name = "posts.load-all-posts", contextualName = "post.find-all")
CommandLineRunner commandLineRunner(JsonPlaceholderService jsonPlaceholderService) {
return args -> {
jsonPlaceholderService.findAll();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.huyvu.hicha.hichabusiness.service;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

@Service
public class JsonPlaceholderServiceImpl{
@Bean
JsonPlaceholderService jsonPlaceholderService() {
RestClient restClient = RestClient.create("https://jsonplaceholder.typicode.com");
HttpServiceProxyFactory fact = HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient)).build();
return fact.createClient(JsonPlaceholderService.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.huyvu.hicha.hichabusiness.controller;

import io.huyvu.hicha.hichabusiness.service.JsonPlaceholderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -15,7 +16,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(PostController.class)
@WebMvcTest({PostController.class, JsonPlaceholderServiceImpl.class})
@AutoConfigureMockMvc
@Slf4j
class PostControllerTest {
Expand Down

0 comments on commit b632166

Please sign in to comment.