BAEL-5649 (#12677)
* add hexagonal architecture example project * rename class * add unit test and integration test * add new line * reorder pom * remove maven wrapper * [BAEL-5649] add article code * remove files added by mistake * get parameter type using reflection and serioalize JSON into POJOs * clean code * clean code * reformat * implement PR feedback * reformat with contiunation space eq to 2 * replace UserDto with firstName and lastName
This commit is contained in:
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.jsonargs;
|
||||
|
||||
public class AddressDto {
|
||||
|
||||
private String streetName;
|
||||
private String streetNumber;
|
||||
private String postalCode;
|
||||
private String city;
|
||||
private String country;
|
||||
|
||||
public String getStreetName() {
|
||||
return streetName;
|
||||
}
|
||||
|
||||
public void setStreetName(String streetName) {
|
||||
this.streetName = streetName;
|
||||
}
|
||||
|
||||
public String getStreetNumber() {
|
||||
return streetNumber;
|
||||
}
|
||||
|
||||
public void setStreetNumber(String streetNumber) {
|
||||
this.streetNumber = streetNumber;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return postalCode;
|
||||
}
|
||||
|
||||
public void setPostalCode(String postalCode) {
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Address{" + "streetName='" + streetName + '\'' + ", streetNumber='" + streetNumber + '\'' + ", postalCode='" + postalCode + '\'' + ", city='" + city + '\'' + ", country='" + country + '\'' + '}';
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.jsonargs;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface JsonArg {
|
||||
String value() default "";
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.jsonargs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
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 com.jayway.jsonpath.JsonPath;
|
||||
|
||||
public class JsonArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private static final String JSON_BODY_ATTRIBUTE = "JSON_REQUEST_BODY";
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return parameter.hasParameterAnnotation(JsonArg.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
||||
String body = getRequestBody(webRequest);
|
||||
String jsonPath = Objects.requireNonNull(Objects.requireNonNull(parameter.getParameterAnnotation(JsonArg.class))
|
||||
.value());
|
||||
Class<?> parameterType = parameter.getParameterType();
|
||||
return JsonPath.parse(body)
|
||||
.read(jsonPath, parameterType);
|
||||
}
|
||||
|
||||
private String getRequestBody(NativeWebRequest webRequest) {
|
||||
HttpServletRequest servletRequest = Objects.requireNonNull(webRequest.getNativeRequest(HttpServletRequest.class));
|
||||
String jsonBody = (String) servletRequest.getAttribute(JSON_BODY_ATTRIBUTE);
|
||||
if (jsonBody == null) {
|
||||
try {
|
||||
jsonBody = IOUtils.toString(servletRequest.getInputStream());
|
||||
servletRequest.setAttribute(JSON_BODY_ATTRIBUTE, jsonBody);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return jsonBody;
|
||||
}
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.jsonargs;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@PostMapping("/process/custom")
|
||||
public ResponseEntity process(@JsonArg("firstName") String firstName, @JsonArg("address.city") String city) {
|
||||
/* business processing */
|
||||
return ResponseEntity.ok()
|
||||
.body(String.format("{\"firstName\": %s, \"city\" : %s}", firstName, city));
|
||||
}
|
||||
|
||||
@PostMapping("/process")
|
||||
public ResponseEntity process(@RequestBody UserDto user) {
|
||||
/* business processing */
|
||||
return ResponseEntity.ok()
|
||||
.body(user.toString());
|
||||
}
|
||||
|
||||
@PostMapping("/process/custompojo")
|
||||
public ResponseEntity process(@JsonArg("firstName") String firstName, @JsonArg("lastName") String lastName, @JsonArg("address") AddressDto address) {
|
||||
/* business processing */
|
||||
return ResponseEntity.ok()
|
||||
.body(String.format("{\"firstName\": %s, \"lastName\": %s, \"address\" : %s}", firstName, lastName, address));
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.jsonargs;
|
||||
|
||||
public class UserDto {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String age;
|
||||
private AddressDto address;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(String age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public AddressDto getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(AddressDto address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", age='" + age + '\'' + ", address=" + address + '}';
|
||||
}
|
||||
}
|
||||
+15
-3
@@ -1,7 +1,10 @@
|
||||
package com.baeldung.spring.web.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
@@ -11,18 +14,26 @@ import org.springframework.web.servlet.view.BeanNameViewResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
import com.baeldung.jsonargs.JsonArgumentResolver;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/")
|
||||
.setViewName("index");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
JsonArgumentResolver jsonArgumentResolver = new JsonArgumentResolver();
|
||||
argumentResolvers.add(jsonArgumentResolver);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
bean.setViewClass(JstlView.class);
|
||||
bean.setPrefix("/WEB-INF/view/");
|
||||
bean.setSuffix(".jsp");
|
||||
@@ -41,9 +52,10 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BeanNameViewResolver beanNameViewResolver(){
|
||||
public BeanNameViewResolver beanNameViewResolver() {
|
||||
BeanNameViewResolver beanNameViewResolver = new BeanNameViewResolver();
|
||||
beanNameViewResolver.setOrder(1);
|
||||
return beanNameViewResolver;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user