记录 tech road 点滴的收获, 日后整理的资料 2020-08-09
1. Spring IoC ,Strategy 的子类实现,在 customer 的setter 参数中 Collection<StrategyInterface> strategy>
可将所有具体的实现注入。
2. Java8 stream 的 flatMap , 双 map 处理双集合
3 Optional<T> 结合 findAny() ,
isPresent() 返回true或false,
ifPresent ( Consumer<T> block )会在值存在的时候执行, T 与 Optional<T> 一致。
4, Class Enum
Spring boot web
1. Spring boot context runner
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 SpringBootApplication 2 public class PracticeApplication { 3 4 public static void main(String[] args) { 5 6 SpringApplication.run(PracticeApplication.class,args); 7 8 /* 9 ConfigurableApplicationContext ctx = SpringApplication.run(PracticeApplication.class, args); 10 11 Calculator calculator = ctx.getBean(Calculator.class); 12 calculator.calculate(123,334,'+'); 13 calculator.calculate(123, 123, '*'); 14 */ 15 16 } 17 18 /* @Bean 19 public ApplicationRunner calculationRunner(Calculator calculator){ 20 return args -> { 21 calculator.calculate(124, 123, '+'); 22 calculator.calculate(123, 22, '*'); 23 }; 24 }*/ 25 26 /* @Bean 27 public ApplicationRunner calculationRunner(Calculator calculator, 28 @Value("${lhs : 1}") int lhs, 29 @Value("${rhs : 1}") int rhs, 30 @Value("${op : '+'}") char op){ 31 return args ->{ 32 calculator.calculate(lhs, rhs, op);}; 33 }*/ 34 }
2. log
3. spring mvc
MockMvc test
1 package org.techroad.springboot.practice.cp3_springMVC.example; 2 3 import org.junit.jupiter.api.Test; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; 6 import org.springframework.boot.test.mock.mockito.MockBean; 7 import org.springframework.test.web.servlet.MockMvc; 8 import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 9 10 import java.util.Arrays; 11 import java.util.Optional; 12 13 import static org.hamcrest.collection.IsCollectionWithSize.hasSize; 14 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; 15 import static org.hamcrest.core.IsEqual.equalTo; 16 17 import static org.mockito.ArgumentMatchers.anyString; 18 import static org.mockito.Mockito.when; 19 20 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 21 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; 22 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 23 24 @WebMvcTest(BookController.class) 25 class BookControllerTest { 26 27 @Autowired 28 private MockMvc mockMvc; 29 30 @MockBean 31 private BookService bookService; 32 33 @Test 34 public void shouldReturnListOfBooks() throws Exception{ 35 when(bookService.findAll()).thenReturn( 36 Arrays.asList( 37 new Book("123","Spring 5 Recipes", "Marten denium, Josh long"), 38 new Book("345","Pro spring mvc","Marten") 39 ) 40 ); 41 42 mockMvc.perform(get("/books")) 43 .andExpect(status().isOk()) 44 .andExpect(jsonPath("$",hasSize(2))) 45 .andExpect( jsonPath("$[*].isbn",containsInAnyOrder("123","345"))) 46 .andExpect( jsonPath("$[*].title",containsInAnyOrder("Spring 5 Recipes","Pro spring mvc"))); 47 } 48 49 @Test 50 public void shouldReturn404WhenBookNotFound() throws Exception{ 51 when(bookService.find(anyString())).thenReturn(Optional.empty()); 52 53 mockMvc.perform(get("/book/123")).andExpect( 54 status().isNotFound() 55 ); 56 } 57 58 @Test 59 public void shouldReturnBookWhenFound() throws Exception{ 60 when(bookService.find(anyString())).thenReturn( 61 Optional.of( 62 new Book("123", "Spring 5 Recipes", "Marten") 63 ) 64 ); 65 66 mockMvc.perform(get("/books/123")) 67 .andExpect(status().isOk()) 68 .andExpect(jsonPath("$.isbn",equalTo("123"))) 69 .andExpect( jsonPath("$.title",equalTo("Spring 5 Recipes"))); 70 } 71 72 }
2. thymeleaf config
resources/templates/ xx.html
模型中包含错误页面的属性
timestamp 错误发生的时间
status 状态代码
error 错误原因
exception 根异常的类名(如果已配置)
message 异常消息
errors 由 BindingResult 产生的任何 ObjectError 对象(仅当使用了消息绑定或消息验证时)
trace 异常堆栈跟踪信息(如果已配置)
path 引发异常的 URL 路径
由 ErrorAttributes 组件提供, SpringBoot 默认 defaultErrorAttributes, 可自定义或扩展。