2

I am making GET request and it has 2 parameters or basically an array

param {
paramNo:1,
from:mobile,
to:server
}
param {
paramNo:2,
from:server,
to:mobile
}

In my controller I have captured it as

public  @ResponseBody SearchResponse serverSearch(@RequestParam List<String> param) throws Exception {
     ObjectMapper mapper = new ObjectMapper();
     List<SearchInfo> searchInfo = mapper.readValue(param,new TypeReference<List<SearchInfo>>(){});
}

mapper.readValue does not take a List. It is throwing compilation error.

Question

  1. I would prefer to retrieve it as (@RequestParam List param) rather than invoking objectMapper. What should I do convert it directly to List
  2. how do I convert List to List?
2
  • How to convert List<A> to List<B>? Loop the list and convert one item at a time, if there's no collection converter. Commented Jan 4, 2014 at 22:02
  • I was hoping I can avoid looping expect the mapping to be automatically. Even if I have to loop, I still cannot do @RequestParam List<Param>, I see the following exception org.springframework.beans.ConversionNotSupportedException. Commented Jan 6, 2014 at 17:49

1 Answer 1

2

You will have to use arrays instead of lists initially, but you can easily do a: List<SearchInfo> params = Arrays.asList(myArray);

Converting JSON

If your parameters are valid JSON, as it seems from your example, it's quite simple to convert into a custom object, see here.


Converting something else

Otherwise you can create a custom formatter with Spring that will format the Strings that come from your request parameters into your custom objects. Basically you'll have to first create a class that registers the type of objects to format and which class does the formatting:

import org.springframework.format.FormatterRegistrar;
import org.springframework.format.FormatterRegistry;

public class SearchInfoFormatterRegistrar implements FormatterRegistrar {
  @Override
  public void registerFormatters(FormatterRegistry registry) {
    registry.addFormatterForFieldType(SearchInfo.class, new SearchInfoFormatter());
  }
}

Then implement the class doing the formatting (mind you, this is not just casting an object to a different type, you actually have to use some logic):

import org.springframework.format.Formatter;

public class SearchInfoFormatter implements Formatter<SearchInfo> {
  @Override
  public String print(SearchInfo info, Locale locale) {
    // Format SearchInfo into String here.
  }

  @Override
  public SearchInfo parse(String text, Locale locale) {
    // Format String into SearchInfo here.
  }
}

Finally, you add them into your configuration:

<bean name="conversionService"
      class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatterRegistrars">
        <set>
            <bean class="org.my.SearchInfoFormatterRegistrar" />
        </set>
    </property>
</bean>
Sign up to request clarification or add additional context in comments.

Comments

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.