[JAVA] Map GET requests to complex objects in Spring.

What you want to do (purpose)

Map Spring GET request (URL parameter) to complex object (Bean).

--Nested structure --List structure

Conclusion

Do not use annotations for Mapping. Or Use @ModelAttribute.

environment

Spring Boot:2.1.6

Other libraries used

lombok

Textile verification source

Mapping destination

Parent object

@Data
@ToString
public class ComplexBean {
  private String hoge;
  private ChildBean childBean;
  private List<ItemBean> itemBeans;
}

Child object for nesting

@Data
@ToString
public class ChildBean {
  private String piyo;
}

Object for list

@Data
@ToString
public class ItemBean {
  private String fuga;
}

controller

@RestController
public class DemoRestController {

  @GetMapping(value = "/param")
  @ResponseBody
  public String getParam(@RequestParam ComplexBean bean) {
    System.out.println(bean.toString());
    return bean.toString();
  }

  @GetMapping(value = "/model")
  @ResponseBody
  public String getModel(@ModelAttribute ComplexBean bean) {
    System.out.println(bean.toString());
    return bean.toString();
  }

  @GetMapping(value = "/direct")
  @ResponseBody
  public String getDirect(ComplexBean bean) {
    System.out.println(bean.toString());
    return bean.toString();
  }
}

Verification

Restlet Client-Access each endpoint using REST API Testing

URL parameters to validate

hoge=hoge&childBean.piyo=piyo&itemBeans[0].fuga=fuga0&itemBeans[2].fuga=fuga2

result

When using @RequestParam (http://localhost:8080/param?hoge=hoge&childBean.piyo=piyo&itemBeans[0].fuga=fuga0&itemBeans[2].fuga=fuga2) Since the mapping information is insufficient, parsing cannot be performed and an error occurs.

Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required ComplexBean parameter 'bean' is not present]

image.png

When using @ModelAttribute (http://localhost:8080/model?hoge=hoge&childBean.piyo=piyo&itemBeans[0].fuga=fuga0&itemBeans[2].fuga=fuga2) Mapped as expected. image.png

Without annotation (http://localhost:8080/direct?hoge=hoge&childBean.piyo=piyo&itemBeans[0].fuga=fuga0&itemBeans[2].fuga=fuga2) Mapped as expected. image.png

Summary

Even with URL parameters that have the following structure --Nested structure --List structure

Do not use annotations for Mapping. Or Use @ModelAttribute.

If so, it can be mapped ~~, but let's review the design when it becomes necessary to map to such an object with a GET request ~~. If the sequence number is skipped as in verification, null is set for the skipped branch number. (⇒ It will not be an empty object)

Recommended Posts

Map GET requests to complex objects in Spring.
Get cookies in Spring
How to use Lombok in Spring
Get Null-safe Map values in Java
How to get parameters in Spark
How to get date data in Ruby
Convert request parameter to Enum in Spring
To write Response data directly in Spring
How to map tsrange type in Hibernate
How to get the date in java
How to get the setting value (property value) from the database in Spring Framework
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to get keycloak credentials in interceptor class
How to get Class from Element in Java
How to add a classpath in Spring Boot
Library "OSHI" to get system information in Java
How to bind to property file in Spring Boot
How to define multiple orm.xml in Spring4, JPA2.1
I want to get the value in Ruby
[Java] How to get the key and value stored in Map by iterative processing
Get multiple Resources that match the pattern in spring
ARKit: Name objects in Reality Composer, get names in Swift
Automatically map DTOs to entities with Spring Boot API
How to get information about associated tables in many-to-many tables
How to create a Spring Boot project in IntelliJ
Source used to get the redirect source URL in Java
I tried to get started with Spring Data JPA
Get Locale objects for all locales available in Java
Where to get lost in specifications with Spark routing
How to use CommandLineRunner in Spring Batch of Spring Boot
How to test file upload screen in Spring + Selenium
Introduce swagger-ui to REST API implemented in Spring Boot
How to use In-Memory Job repository in Spring Batch
Fargate to get a rough idea in relation to EC2
I want to get along with Map [Java beginner]