Skip to content

Commit 34e0bfc

Browse files
committed
argumentresolver example
1 parent dfbc044 commit 34e0bfc

12 files changed

Lines changed: 88 additions & 27 deletions

File tree

build-all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ build_gradle_module() {
3030

3131
chmod +x gradlew
3232

33+
build_gradle_module "spring-boot/argumentresolver"
3334
build_gradle_module "solid"
3435
build_gradle_module "spring-boot/data-migration/flyway"
3536
build_gradle_module "reactive"
54.9 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

spring-boot/argumentresolver/src/main/java/io/reflectoring/argumentresolver/ErrorHandler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import org.springframework.http.ResponseEntity;
44
import org.springframework.web.bind.annotation.ControllerAdvice;
55
import org.springframework.web.bind.annotation.ExceptionHandler;
6-
import org.springframework.web.client.HttpStatusCodeException;
76

87
@ControllerAdvice
98
class ErrorHandler {
109

11-
@ExceptionHandler(HttpStatusCodeException.class)
12-
ResponseEntity<?> handleHttpStatusCodeException(HttpStatusCodeException e) {
10+
@ExceptionHandler(NotFoundException.class)
11+
ResponseEntity<?> handleHttpStatusCodeException(NotFoundException e) {
1312
return ResponseEntity.status(e.getStatusCode()).build();
1413
}
1514

spring-boot/argumentresolver/src/main/java/io/reflectoring/argumentresolver/Repository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import lombok.Value;
55

66
@Value
7-
public class Repository {
7+
class Repository {
88

99
private final Long id;
1010
private final String slug;

spring-boot/argumentresolver/src/main/java/io/reflectoring/argumentresolver/RepositoryArgumentResolver.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.reflectoring.argumentresolver;
22

33
import java.util.Optional;
4-
import java.util.regex.Matcher;
54
import java.util.regex.Pattern;
65
import lombok.RequiredArgsConstructor;
76
import org.springframework.core.MethodParameter;
@@ -14,8 +13,6 @@
1413
@RequiredArgsConstructor
1514
class RepositoryArgumentResolver implements HandlerMethodArgumentResolver {
1615

17-
private static final Pattern SLUG_PATTERN = Pattern.compile("^/([^/]*).*$");
18-
1916
private final RepositoryFinder repositoryFinder;
2017

2118
@Override
@@ -32,20 +29,9 @@ public Object resolveArgument(
3229

3330
String requestPath = ((ServletWebRequest) webRequest).getRequest().getPathInfo();
3431

35-
Matcher matcher = SLUG_PATTERN.matcher(requestPath);
36-
37-
if (!matcher.matches()) {
38-
throw new IllegalArgumentException(String.format(
39-
"Cannot resolve argument of type Site. Expecting the slug to be the first part of the request path (%s).",
40-
requestPath));
41-
}
42-
43-
String slug = matcher.group(1);
44-
if (slug == null || slug.isBlank()) {
45-
throw new IllegalArgumentException(String.format(
46-
"Cannot resolve argument of type Site. Slug is empty (request path: %s).",
47-
requestPath));
48-
}
32+
String slug = requestPath
33+
.substring(0, requestPath.indexOf("/", 1))
34+
.replaceAll("^/", "");
4935

5036
Optional<Repository> repository = repositoryFinder.findBySlug(slug);
5137

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.reflectoring.argumentresolver;
2+
3+
import lombok.Value;
4+
5+
@Value
6+
class RepositoryId {
7+
8+
private final long value;
9+
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.reflectoring.argumentresolver;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
class RepositoryIdConverter implements Converter<String, RepositoryId> {
8+
9+
@Override
10+
public RepositoryId convert(String source) {
11+
return new RepositoryId(Long.parseLong(source));
12+
}
13+
}

spring-boot/argumentresolver/src/test/java/io/reflectoring/argumentresolver/RepositoryArgumentResolverTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.springframework.boot.test.mock.mockito.MockBean;
1212
import org.springframework.test.web.servlet.MockMvc;
1313

14-
@WebMvcTest(controllers = TestController.class)
14+
@WebMvcTest(controllers = RepositoryArgumentResolverTestController.class)
1515
class RepositoryArgumentResolverTest {
1616

1717
@Autowired
@@ -26,7 +26,7 @@ void resolvesSiteSuccessfully() throws Exception {
2626
given(repositoryFinder.findBySlug("my-repo"))
2727
.willReturn(Optional.of(new Repository(1L, "my-repo")));
2828

29-
mockMvc.perform(get("/my-repo/foo"))
29+
mockMvc.perform(get("/my-repo/listContributors"))
3030
.andExpect(status().isOk());
3131
}
3232

@@ -36,7 +36,7 @@ void notFoundOnUnknownSlug() throws Exception {
3636
given(repositoryFinder.findBySlug("unknownSlug"))
3737
.willReturn(Optional.empty());
3838

39-
mockMvc.perform(get("/unknownSlug/foo"))
39+
mockMvc.perform(get("/unknownSlug/listContributors"))
4040
.andExpect(status().isNotFound());
4141
}
4242

spring-boot/argumentresolver/src/test/java/io/reflectoring/argumentresolver/TestController.java renamed to spring-boot/argumentresolver/src/test/java/io/reflectoring/argumentresolver/RepositoryArgumentResolverTestController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
67
import org.springframework.web.bind.annotation.RestController;
78

89
@RestController
9-
class TestController {
10+
@RequestMapping(path = "/{repositorySlug}")
11+
class RepositoryArgumentResolverTestController {
1012

11-
@GetMapping("/{slug}/foo")
12-
String getSomething(Repository repository) {
13+
@GetMapping("/listContributors")
14+
String listContributors(Repository repository) {
1315
assertThat(repository.getId()).isEqualTo(1L);
1416
return "test";
1517
}

0 commit comments

Comments
 (0)