Question

Hope you are doing well.

I have a question that's related to Spring MVC and its controller.

I have following code snippets inside a controller:

@RequestMapping(value = "/booking-curve/history/journeys/{journeyIds}/travel-date-ranges/{travelDateRanges}", method = RequestMethod.GET)
@ResponseBody
public String generateHistoricalBookingCurve(@PathVariable List<Integer> journeyIds, @PathVariable List<Date[]> travelDateRanges) {
    double[] aveBookingLoadFactors = pricingService.calculateAveBookingLoadFactors(journeyIds, travelDateRanges);
    JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ArrayNode array = nodeFactory.arrayNode();
    for (int i = 0; i < aveBookingLoadFactors.length; i++) {
        ObjectNode object = nodeFactory.objectNode();
        object.put("daysBefore", aveBookingLoadFactors.length - 1 - i);
        object.put("aveBookingLoadFactor", aveBookingLoadFactors[i]);
        array.add(object);
    }
    return array.toString();
}

the journeyIds in the url link is made up of comma separated ids, and travelDateRanges in the url link is made up of comma separated from to date pairs. e.g. journeyIds are somethings like 11,22,44 and travelDateRanges are somethings like 2012-11-11to2012-12-01,2014-02-11to2014-05-03.

I want to spring mvc can automatically convert above strings to List'<'Integer'>' and List'<'Date[]'>' as shown in my method above. I think PropertyEditor or Converter are supposed to work for my case, but I tried without luck or probably I didn't configure them correctly.

Experts here, please shed some light on my questions! Thank you very much!

Best regards,

Was it helpful?

Solution

You will have to provide a Converter and a wrapper class ...

@Component
public class YourConverter implements Converter<YourIntWrapper, YourDateWrapper> {

    @Override
    public YourDateWrapper convert(YourIntWrapper source) {
                // do your stuff
        return yourOutWrapper;
    }
}

and register it

<bean id="conversionService"
          class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.yourpackage.YourConverter"/>
            </set>
        </property>
    </bean>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top