0

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,

2

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. You've really been helpful.
@YEBO thanks - votes and accepts are the normal way to express gratitude
I voted it and accepted it. Thanks for your reminding, just because I am not quite familiar with Stackoverflow convention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.