SpringBoot后台接收前台的字符串数据

 

需求

将前台传入的字符串数据转为int类型。

操作

在pom.xml中添加引用。

 

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>

 

在java类中导入引用。

 

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;

 

不同于接收其他数据,接收String类型的数据,不可以直接@GetMapping(“/{rowId}”),需要使用RequestMapping。

这里ids传入的数据为ids={“1,2,3”…}

@RequestMapping(value = "/Ids/{Ids}", method = {RequestMethod.GET})
@ResponseBody
public R getByIds(@PathVariable("Ids") String ids) {
  List<String> idsStringList = Arrays.asList(ids.split(","));
  List<Integer> idsList = new ArrayList<>();
  CollectionUtils.collect(idsStringList, new Transformer() {
    public Object transform(Object o) {
      return Integer.valueOf(o.toString());
    }
  }, idsList);
  return new R<>(materialHouseService.listByIds(idsList));
}

最终输出的结果样式为int类型的1,2,3。

结果

postman测试结果正确,over。

image

posted @ 2019-04-04 17:23  Phyday  阅读(5446)  评论(0编辑  收藏  举报