简体   繁体   English

Spring MVC REST requestParam处理同一个键的多个值

[英]Spring MVC REST requestParam to handle multiple values for the same key

I am making GET request and it has 2 parameters or basically an array 我正在执行GET请求,它有2个参数或基本上是一个数组

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. mapper.readValue不接受列表。 It is throwing compilation error. 它抛出编译错误。

Question

  1. I would prefer to retrieve it as (@RequestParam List param) rather than invoking objectMapper. 我宁愿将其检索为(@RequestParam List param),而不是调用objectMapper。 What should I do convert it directly to List 我应该怎么做直接将其转换为列表
  2. how do I convert List to List? 如何将列表转换为列表?

You will have to use arrays instead of lists initially , but you can easily do a: List<SearchInfo> params = Arrays.asList(myArray); 最初 ,您将不得不使用数组而不是列表 ,但是您可以轻松地执行以下操作: List<SearchInfo> params = Arrays.asList(myArray);

Converting JSON 转换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 . 如果您的参数是有效的JSON(如您的示例所示),则将其转换为自定义对象非常简单,请参见此处


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. 否则,您可以使用Spring创建一个自定义格式化程序,该格式化程序会将来自请求参数的字符串格式化为自定义对象。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM