2021-03 spring-15-mvc updated

This commit is contained in:
Александр Оруджев
2021-05-24 12:52:09 +04:00
parent 90665acc51
commit 2dd63c0def
22 changed files with 138 additions and 156 deletions
-24
View File
@@ -1,24 +0,0 @@
target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
-11
View File
@@ -1,11 +0,0 @@
POST http://localhost:8080/person
Content-Type: application/json
{
"id": "1",
"name": "Gogol'"
}
###
GET http://localhost:8080/person
@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>ru.otus</groupId>
<artifactId>spring-mvc-solution-4</artifactId>
<artifactId>spring-mvc-demo</artifactId>
<version>1.0</version>
<parent>
@@ -0,0 +1,22 @@
package ru.otus.spring.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import ru.otus.spring.rest.SystemInfoMethodArgumentResolver;
import ru.otus.spring.service.SystemInfoService;
import java.util.List;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private SystemInfoService systemInfoService;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new SystemInfoMethodArgumentResolver(systemInfoService));
}
}
@@ -0,0 +1,19 @@
package ru.otus.spring.domain;
public class SystemInfo {
private final String osName;
private final String timeZone;
private final String osArch;
private final int processorsCount;
public SystemInfo(String osName, String timeZone, String osArch, int processorsCount) {
this.osName = osName;
this.timeZone = timeZone;
this.osArch = osArch;
this.processorsCount = processorsCount;
}
public String getOsName() {
return osName;
}
}
@@ -0,0 +1,14 @@
package ru.otus.spring.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.otus.spring.domain.SystemInfo;
@RestController
public class SystemInfoController {
@GetMapping("/server/system/info")
public SystemInfo getServerSystemInfo(SystemInfo systemInfo) {
return systemInfo;
}
}
@@ -0,0 +1,31 @@
package ru.otus.spring.rest;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import ru.otus.spring.domain.SystemInfo;
import ru.otus.spring.service.SystemInfoService;
public class SystemInfoMethodArgumentResolver implements HandlerMethodArgumentResolver {
private final SystemInfoService systemInfoService;
public SystemInfoMethodArgumentResolver(SystemInfoService systemInfoService) {
this.systemInfoService = systemInfoService;
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterType().equals(SystemInfo.class);
}
@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
return systemInfoService.getSystemInfo();
}
}
@@ -0,0 +1,17 @@
package ru.otus.spring.service;
import org.springframework.stereotype.Service;
import ru.otus.spring.domain.SystemInfo;
@Service
public class SystemInfoService {
//
public SystemInfo getSystemInfo(){
String osName = System.getProperty("os.name");
String timeZone = System.getProperty("user.timezone");
String osArch = System.getProperty("os.arch");
int processorsCount = Runtime.getRuntime().availableProcessors();;
return new SystemInfo(osName, timeZone, osArch, processorsCount);
//HandlerMethodArgumentResolver
}
}
@@ -0,0 +1,34 @@
package ru.otus.spring.rest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import ru.otus.spring.domain.SystemInfo;
import ru.otus.spring.service.SystemInfoService;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
@WebMvcTest(SystemInfoController.class)
@Import(SystemInfoService.class)
class SystemInfoControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
private ObjectMapper mapper;
@Autowired
private SystemInfoService systemInfoService;
@Test
void shouldReturnCorrectServerSystemInfo() throws Exception {
SystemInfo expectedSystemInfo = systemInfoService.getSystemInfo();
mvc.perform(get("/server/system/info"))
.andExpect(content().json(mapper.writeValueAsString(expectedSystemInfo)));
}
}
@@ -1,24 +0,0 @@
target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
@@ -1,24 +0,0 @@
target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
@@ -1,24 +0,0 @@
target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
@@ -1,24 +0,0 @@
target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
@@ -1,24 +0,0 @@
target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/