Skip to content

Commit 55113dd

Browse files
authored
Merge pull request eugenp#966 from vicmosin/master
BAEL-529 Spring data binders
2 parents f39332f + 753b309 commit 55113dd

8 files changed

Lines changed: 164 additions & 7 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.config;
2+
3+
import org.baeldung.web.resolver.HeaderVersionArgumentResolver;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
import java.util.List;
9+
10+
@Configuration
11+
public class WebConfig extends WebMvcConfigurerAdapter {
12+
13+
@Override
14+
public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> argumentResolvers) {
15+
argumentResolvers.add(new HeaderVersionArgumentResolver());
16+
}
17+
}

‎spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package org.baeldung.controller;
22

33
import org.baeldung.domain.GenericEntity;
4+
import org.baeldung.domain.Modes;
5+
import org.baeldung.web.resolver.Version;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
48
import org.springframework.web.bind.annotation.PathVariable;
59
import org.springframework.web.bind.annotation.RequestMapping;
610
import org.springframework.web.bind.annotation.RequestMethod;
711
import org.springframework.web.bind.annotation.RestController;
812

13+
import java.time.LocalDateTime;
914
import java.util.ArrayList;
1015
import java.util.List;
1116

@@ -35,4 +40,21 @@ public GenericEntity addEntity(GenericEntity entity) {
3540
public GenericEntity findById(@PathVariable Long id) {
3641
return entityList.stream().filter(entity -> entity.getId().equals(id)).findFirst().get();
3742
}
43+
44+
@RequestMapping("/entity/findbydate/{date}")
45+
public GenericEntity findByDate(@PathVariable("date") LocalDateTime date) {
46+
return entityList.stream().findFirst().get();
47+
}
48+
49+
@RequestMapping("/entity/findbymode/{mode}")
50+
public GenericEntity findByEnum(@PathVariable("mode") Modes mode) {
51+
return entityList.stream().findFirst().get();
52+
}
53+
54+
@RequestMapping("/entity/findbyversion")
55+
public ResponseEntity findByVersion(@Version String version) {
56+
return version != null
57+
? new ResponseEntity(entityList.stream().findFirst().get(), HttpStatus.OK)
58+
: new ResponseEntity(HttpStatus.NOT_FOUND);
59+
}
3860
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.baeldung.converter;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
import org.springframework.core.convert.converter.ConverterFactory;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {
9+
10+
private static class StringToEnumConverter<T extends Enum> implements Converter<String, T> {
11+
12+
private Class<T> enumType;
13+
14+
public StringToEnumConverter(Class<T> enumType) {
15+
this.enumType = enumType;
16+
}
17+
18+
public T convert(String source) {
19+
return (T) Enum.valueOf(this.enumType, source.trim());
20+
}
21+
}
22+
23+
@Override
24+
public <T extends Enum> Converter<String, T> getConverter(final Class<T> targetType) {
25+
return new StringToEnumConverter(targetType);
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.converter;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
import org.springframework.stereotype.Component;
5+
6+
import java.time.LocalDateTime;
7+
import java.time.format.DateTimeFormatter;
8+
import java.time.format.DateTimeParseException;
9+
10+
@Component
11+
public class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
12+
13+
@Override
14+
public LocalDateTime convert(final String source) {
15+
try {
16+
return LocalDateTime.parse(source, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
17+
} catch (DateTimeParseException e) {
18+
throw new IllegalArgumentException("Couldn't parse value");
19+
}
20+
}
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.baeldung.domain;
2+
3+
public enum Modes {
4+
5+
Alpha, Beta;
6+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.web.resolver;
2+
3+
import org.springframework.core.MethodParameter;
4+
import org.springframework.stereotype.Component;
5+
import org.springframework.web.bind.support.WebDataBinderFactory;
6+
import org.springframework.web.context.request.NativeWebRequest;
7+
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
8+
import org.springframework.web.method.support.ModelAndViewContainer;
9+
10+
import javax.servlet.http.HttpServletRequest;
11+
12+
@Component
13+
public class HeaderVersionArgumentResolver implements HandlerMethodArgumentResolver {
14+
15+
@Override
16+
public boolean supportsParameter(final MethodParameter methodParameter) {
17+
return methodParameter.getParameterAnnotation(Version.class) != null;
18+
}
19+
20+
@Override
21+
public Object resolveArgument(final MethodParameter methodParameter, final ModelAndViewContainer modelAndViewContainer, final NativeWebRequest nativeWebRequest, final WebDataBinderFactory webDataBinderFactory) throws Exception {
22+
HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();
23+
24+
return request.getHeader("Version");
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.web.resolver;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.PARAMETER)
10+
public @interface Version {
11+
}

‎spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.baeldung;
22

3-
import org.baeldung.domain.GenericEntity;
4-
import org.baeldung.repository.GenericEntityRepository;
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.hamcrest.Matchers.hasSize;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
6+
57
import org.junit.Before;
68
import org.junit.Test;
79
import org.junit.runner.RunWith;
@@ -18,11 +20,6 @@
1820

1921
import java.nio.charset.Charset;
2022

21-
import static org.hamcrest.Matchers.hasSize;
22-
import static org.junit.Assert.assertEquals;
23-
import static org.junit.Assert.assertNotNull;
24-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
25-
2623
@RunWith(SpringJUnit4ClassRunner.class)
2724
@SpringApplicationConfiguration(classes = Application.class)
2825
@WebAppConfiguration
@@ -42,4 +39,34 @@ public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect()
4239

4340
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4)));
4441
}
42+
43+
@Test
44+
public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception {
45+
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
46+
47+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30"))
48+
.andExpect(MockMvcResultMatchers.status().isOk())
49+
.andExpect(MockMvcResultMatchers.content().contentType(contentType))
50+
.andExpect(jsonPath("$.id", equalTo(1)));
51+
}
52+
53+
@Test
54+
public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception {
55+
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
56+
57+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", "Alpha"))
58+
.andExpect(MockMvcResultMatchers.status().isOk())
59+
.andExpect(MockMvcResultMatchers.content().contentType(contentType))
60+
.andExpect(jsonPath("$.id", equalTo(1)));
61+
}
62+
63+
@Test
64+
public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception {
65+
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
66+
67+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0"))
68+
.andExpect(MockMvcResultMatchers.status().isOk())
69+
.andExpect(MockMvcResultMatchers.content().contentType(contentType))
70+
.andExpect(jsonPath("$.id", equalTo(1)));
71+
}
4572
}

0 commit comments

Comments
 (0)