将@RequestParam作为列表是不可能的?

问题描述 投票:3回答:3

我有一个像这样的控制器:

@RequestMapping(value="/selectTimeSpaceBusRunList.do")
public ModelAndView selectTimeSpaceBusRunList(Model model, HttpServletRequest request,
        @RequestParam("search_date") String search_date,
        @RequestParam(value="compid", required=false, defaultValue="") String compid,
        @RequestParam(value="routeid", required=true) String routeid,
        @RequestParam(value="busList", required=true) List<String> busList)
{
    ModelAndView mv = new ModelAndView();
    Map<String, List<TbDmhTmspaceChartVO>> resultMap = new HashMap<String, List<TbDmhTmspaceChartVO>>();

    try {
        TbDmhTmspaceChartVO vo = new TbDmhTmspaceChartVO();
        search_date = search_date.replaceAll("-", "");
        vo.setSearch_start_date(search_date + "000000");
        vo.setSearch_end_date(search_date + "235959");
        vo.setCompid(compid);
        vo.setRouteid(routeid);
        vo.setBusList(busList);
        List<TbDmhTmspaceChartVO> resultList = runService.selectTimeSpaceBusRunList(vo);
        resultMap.put("resultList", resultList);
    } catch (Exception e) {
        logger.error("##selectTimeSpaceBusRunList exception " + e.toString());
    }
    mv.addAllObjects(resultMap);
    mv.setViewName("jsonView");
    return mv;
}

我有这样的VO:

public class TbDmhTmspaceChartVO {
private String run_enddt;       //  date                yes     1   
private String busid;           //  number(9,0)         yes     2   
private String run_startdt;     //  date                yes     3   
private String routeid;         //  number(9,0)         yes     4   
private String compid;          //  number(6,0)         yes     5   
private String carregno;        //  varchar2(12 byte)   yes     6   
private String runord;          //  number(4,0)         yes     7   
private String start_pathseq;   //  number(5,0)         yes     8   
private String end_pathseq;     //  number(5,0)         yes     9   
private String total_bstopcnt;  //  number(10,0)        yes     10  
private String seq_list;        //  varchar2(4000 byte) yes     11  
private String node_list;       //  varchar2(4000 byte) yes     12  
private String hms_list;        //  varchar2(4000 byte) yes     13  
private String coll_list;       //  varchar2(4000 byte) yes     14  
private String cross_pass_cnt;  //  number(10,0)        yes     15  
private String seq_list_1;      //  varchar2(4000 byte) yes     16  
private String node_list_1;     //  varchar2(4000 byte) yes     17  
private String hms_list_1;      //  varchar2(4000 byte) yes     18  
private String coll_list_1;     //  varchar2(4000 byte) yes     19  
private String bstop_dep_cnt;   //  number(10,0)        yes     20  
private String seq_list_3;      //  varchar2(4000 byte) yes     21  
private String node_list_3;     //  varchar2(4000 byte) yes     22  
private String hms_list_3;      //  varchar2(4000 byte) yes     23  
private String coll_list_3;     //  varchar2(4000 byte) yes     24  

private String search_start_date;
private String search_end_date;

private List<String> busList;

// getters and setters...
public List<String> getBusList() {
    return busList;
}
public void setBusList(List<String> busList) {
    this.busList = busList;
}
}

我发送的数据是:

busList: ["7211342", "7015067"]
compid: "166001"
routeid: "165000056"
search_date: "2017-11-06"

我的ajax电话是这样的:

$.ajax({
    type: "POST",
    url: "./run/selectTimeSpaceBusRunList.do",
    data: {
        search_date : input_date,
        compid : $("#busCompany").val(),
        routeid : $("#busRoute").val(),
        busList : selected_bus_list
    },
    dataType : "json",
    beforeSend: null,
    success: success,
    error: null
});

当ajax调用完成后,它会以这样的错误结束:

org.springframework.web.bind.MissingServletRequestParameterException: Required List parameter 'busList' is not present

所以@RequestParam(value="busList", required=true) List<String> busList)一定有问题,但我找不到解决方案。

我的问题是:

  1. List获取@RequestParam变量是不可能的?
  2. 如果不可能,有没有其他方法来获得List变量?
  3. 是否可以立即获得DTO变量?

问候,


UPDATE

我把@RequestParam(value="busList", required=true) List<String> busList)变成了@RequestParam(value="busList", required=false) List<String> busList),现在它工作正常。所以我认为问题在于busList没有以正确的方式发送。

java spring spring-mvc annotations
3个回答
2
投票

将值列表作为URL参数传递的最常见和标准方法是重复它们,即

http://rentacar.com/api/v1/search?make=audi&model=A8&type=6&type=11&type=12&color=RED&color=GREY

现在的问题是在Spring MVC REST端点处理这样的输入?让我们假设您正在开发一个搜索API来寻找可租用的汽车;这是你将如何做到这一点:

public List<Vehicle> search(@RequestParam(value="make", required=false) String make, @RequestParam(value="model", required=false) String model, @RequestParam(value="type", required=false) List<String> types, @RequestParam(value="color", required=false) List<String> colors) {
....
}

https://medium.com/@rasheedamir/spring-mvc-how-to-pass-list-of-values-as-url-parameters-5d57dcac8457


0
投票

你可以这样做。

http://127.0.0.1:8080/test?busList=A1,A2

 @PostMapping("/test")
 public void test(@RequestParam("busList") List<String> busList) {    
      System.out.println(busList); //[A1,A2]
 }

0
投票

当您传递字符串值时。您可以使用busList数组

http://localhost:8080/selectTimeSpaceBusRunList?busList=1,2,3&search_date=20120101&compid=123&routeid=someId

所以控制器端点就像,

@RequestMapping(value = "/selectTimeSpaceBusRunList") 
public void selectTimeSpaceBusRunList(@RequestParam("busList") String[] busList, @RequestParam("search_date") String search_date, @RequestParam("compid") String compid, @RequestParam("routeid") String routeid) {
   //Business logic here.
 }
© www.soinside.com 2019 - 2024. All rights reserved.