04 SpringBoot

This commit is contained in:
Sergey Petrelevich
2020-09-17 17:18:08 +03:00
parent c783b0de8e
commit dcea7d1caa
11 changed files with 42 additions and 43 deletions
@@ -3,12 +3,12 @@ package ru.otus.mainpackage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import ru.otus.mainpackage.configs.YamlProps;
import ru.otus.mainpackage.configs.AppProps;
//http://localhost:8080/actuator/health
@SpringBootApplication
@EnableConfigurationProperties(YamlProps.class)
@EnableConfigurationProperties(AppProps.class)
public class Demo {
public static void main(String[] args) {
@@ -5,7 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Locale;
@ConfigurationProperties(prefix = "application")
public class YamlProps {
public class AppProps {
private String message;
private Locale locale;
@@ -6,7 +6,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
@Configuration
public class Localization {
public class LocalizationConfig {
@Bean
public MessageSource messageSource() {
var ms = new ReloadableResourceBundleMessageSource();
@@ -1,7 +0,0 @@
package ru.otus.mainpackage.welcome;
import java.util.Map;
public interface Greeting {
Map<String, String> sayHello(String name);
}
@@ -2,11 +2,10 @@ package ru.otus.mainpackage.welcome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.otus.mainpackage.configs.YamlProps;
import ru.otus.mainpackage.configs.AppProps;
import java.util.Map;
@@ -14,16 +13,16 @@ import java.util.Map;
@RestController
public class GreetingController {
private static final Logger logger = LoggerFactory.getLogger(GreetingController.class);
private final Greeting greeting;
private final GreetingService greetingService;
public GreetingController(Greeting greeting, YamlProps props) {
this.greeting = greeting;
public GreetingController(GreetingService greetingService, AppProps props) {
this.greetingService = greetingService;
logger.info("ATTENTION! props.getMessage(): {}", props.getMessage());
}
//http://localhost:8080/hello?name=ddd
@GetMapping("/hello")
public Map<String, String> sayHello(@RequestParam(name="name") String name) {
return this.greeting.sayHello(name);
return this.greetingService.sayHello(name);
}
}
@@ -12,16 +12,16 @@ import java.util.Map;
public class GreetingControllerRstyle {
private static final Logger logger = LoggerFactory.getLogger(GreetingControllerRstyle.class);
private final Greeting greeting;
private final GreetingService greetingService;
public GreetingControllerRstyle(Greeting greeting) {
public GreetingControllerRstyle(GreetingService greetingService) {
logger.info("Я НАСТОЯЩИЙ БИН!!!");
this.greeting = greeting;
this.greetingService = greetingService;
}
//http://localhost:8080/hello/jone
@GetMapping(value="/hello/{name}")
public Map<String, String> sayHello(@PathVariable("name") String name) {
return this.greeting.sayHello(name);
return this.greetingService.sayHello(name);
}
}
@@ -1,17 +1,7 @@
package ru.otus.mainpackage.welcome;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class GreetingService implements Greeting {
@Override
public Map<String, String> sayHello(String name) {
Map<String, String> map = new HashMap<>();
map.put(name, "Hello, " + name);
return map;
}
public interface GreetingService {
Map<String, String> sayHello(String name);
}
@@ -0,0 +1,17 @@
package ru.otus.mainpackage.welcome;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class GreetingServiceServiceImpl implements GreetingService {
@Override
public Map<String, String> sayHello(String name) {
Map<String, String> map = new HashMap<>();
map.put(name, "Hello, " + name);
return map;
}
}
@@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import ru.otus.mainpackage.configs.YamlProps;
import ru.otus.mainpackage.configs.AppProps;
import javax.annotation.PostConstruct;
@@ -13,9 +13,9 @@ public class LocalHello {
private static final Logger logger = LoggerFactory.getLogger(LocalHello.class);
private final MessageSource messageSource;
private final YamlProps props;
private final AppProps props;
public LocalHello(MessageSource messageSource, YamlProps props) {
public LocalHello(MessageSource messageSource, AppProps props) {
this.messageSource = messageSource;
this.props = props;
}
@@ -8,8 +8,8 @@ management:
include: info, health, beans
application:
message: "Test Props msg"
locale: ru_RU
message: "Test Props msg@@@"
locale: en
messager:
message: "Hello from Messager"
@@ -24,7 +24,7 @@ public class GreetingControllerTest {
private MockMvc mockMvc;
@MockBean
private Greeting greeting;
private GreetingService greetingService;
@Test
public void sayHelloTest() throws Exception {
@@ -33,12 +33,12 @@ public class GreetingControllerTest {
map.put(name, "Hello, " + name);
String expectedResult = "{\"Mr.Test\":\"Hello, Mr.Test\"}";
when(greeting.sayHello(name)).thenReturn(map);
when(greetingService.sayHello(name)).thenReturn(map);
MockHttpServletResponse result = mockMvc.perform(get(String.format("/hello?name=%s", name)))
.andReturn()
.getResponse();
verify(greeting, times(1)).sayHello(name);
verify(greetingService, times(1)).sayHello(name);
System.out.println();
assertThat(result.getContentAsString()).isEqualTo(expectedResult);
}
@@ -50,12 +50,12 @@ public class GreetingControllerTest {
map.put(name, "Hello, " + name);
String expectedResult = "{\"Mr.Test\":\"Hello, Mr.Test\"}";
when(greeting.sayHello(name)).thenReturn(map);
when(greetingService.sayHello(name)).thenReturn(map);
MockHttpServletResponse result = mockMvc.perform(get(String.format("/hello/%s", name)))
.andReturn()
.getResponse();
verify(greeting, times(1)).sayHello(name);
verify(greetingService, times(1)).sayHello(name);
System.out.println();
assertThat(result.getContentAsString()).isEqualTo(expectedResult);
}