Spring REST - binding GET parameters to nested objects

Since at least Spring 4 you can pass in nested objects separated with "." in the url.

In the OP case it would be for query parameters:

?reservationDateRange.from=2019-04-01&reservationDateRange.to=2019-04-03

This assumes that Date can be parsed from the given string. This may not work to an arbitrary level of nesting but I've tested it works with one additional nested object.


When you pass a POJO as container of data, Spring use the name of the properties for build the query string and with the data that you pass build the pojo through an appropriated converter. This works for planar pojo or in other words without nesting, for this purpose you have provide the your converter. for this reason you cold have a think like below:

public class ReservationCriteria {
    String hotelName;

  Date from;
    Date to;
    //getters-setters omitted
}

@RequestMapping(value = "/reservation",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
public List<Reservation> loadReservations(ReservationCriteria criteria)

    return service.loadReservations(criteria);
}

/reservation?hotelName=value&from=val&to=val

in this way you can benefit of standard converter of SpringMVC.

the your attempt to use a sort of json for codificate the inner object didn't work because Spring by default in query string don't understand this presentation you have provide a converter for this purpose.

Update for answer to Ben's comment:

If you want implement a custom Converter you had implements the org.springframework.core.convert.converter.Converter<S, T> and then register the your new Converter on the Spring Conversion Service.

On xml configuration you can use FormattingConversionServiceFactoryBean and register it on mvc namespace like below:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <mvc:annotation-driven  conversion-service="conversionService"/>

    <context:component-scan base-package="com.springapp.mvc"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <util:list>
                <bean class="com.springapp.mvc.DateRangeToStringConverter"/>
                <bean class="com.springapp.mvc.StringToDateRangeConverter"/>
            </util:list>
        </property>
    </bean>
</beans>

on java config you can extends WebMvcConfigurerAdapter and add you bena like below:

@Configuration
@EnableWebMvc
public class YourWebConfigurationClass extends WebMvcConfigurerAdapter{

    @Override
    public void addFormatters(FormatterRegistry formatterRegistry) {
        formatterRegistry.addConverter(yourConverter());
    }

   ...

}

the your converter can be like below:

public class DateRangeToStringConverter implements Converter<DateRange,String> {

    @Override
    public String convert(DateRange dateRange) {
        return Json.createObjectBuilder().add("fromDate",DateFormatData.DATE_FORMAT.format(dateRange.getFrom()))
                .add("toDate", DateFormatData.DATE_FORMAT.format(dateRange.getTo()))
                .build()
                .toString();
    }

}



public class StringToDateRangeConverter implements Converter<String,DateRange> {


    @Override
    public DateRange convert(String dateRange) {
        DateRange range = new DateRange();
        JsonObject jsonObject = Json.createReader(new StringReader(dateRange)).readObject();

        try {
            range.setFrom(DateFormatData.DATE_FORMAT.parse(jsonObject.getString("fromDate")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            range.setTo(DateFormatData.DATE_FORMAT.parse(jsonObject.getString("toDate")));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println(range);
        return range;
    }

}

in this way you can listgening on the url: http://localhost:8080/reservation?hotelName=myHotel&reservationDateRange={"fromDate":"14.04.2016","toDate":"15.04.2016"}

pleas pay attenction on reservation DateRange field because I encoded it like a json.

I hope that it can help you

Tags:

Spring