如何使用Spring Boot在API Get方法Endpoint中传递多个id

时间:2018-06-01 08:33:56

标签: java spring-boot spring-jdbc

我正在尝试在GET端点中传递多个ID。 例如:如果我的数据库包含10名员工的详细信息,其中ID为主键,并且假设我想在特定点的端点中传递多个ID。这可能吗。 假设:

    http://localhost:8080/api/v/listempoloyee/{1,2,3,4}

{1,2,3,4}是我们想要从数据库中获取的雇员的ID列表。

使用Spring Boot和JDBC是否可行。

2 个答案:

答案 0 :(得分:2)

这应该这样做。

  @GetMapping("/{employeeIds}")
  public void trigger(@PathVariable String employeeIds) {
        List<String> ids = Arrays.asList(employeeIds.split(","));
        ..........
  }

现在您在 ids 字段中有一个ID列表。

答案 1 :(得分:0)

                    you can send a post request to your controller.please follow these steps-:

                    1. **I need to  create a html file because of ajax calling.**
                    <!DOCTYPE html>
                    <html>
                        <head>
                            <title>Ajax Test</title>
                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
                            <script src="/js/ajaxCall.js"></script>
                        </head>

                        <body>
                            <button type="button" class="btn btn-sm btn-update btn-primary" id="send">
                            <span class="glyphicon glyphicon-ok"></span>Send Parameter
                            </button>
                        </body>
                    </html>

                2. **create a js file and create a function for ajax calling.**
                $(document).ready(function() {
                    $("#send").on("click",function(){
                        var paramIds = ["1", "2", "3", "4","5"];
                        var parameterData = JSON.stringify({
                            'paramList' :paramIds
                        });
                        $.ajax({
                            contentType: 'application/json',
                            method:'post',
                            data: parameterData,
                            url: "http://localhost:8080/get",
                            dataType: 'json',
                        }).done(function(data, textStatus, xhr, options) {

                        }).fail(function(xhr, textStatus, errorThrown) {

                        }).always(function(xhr, textStatus) {
                        });
                    })  

                });
            please focus on **JSON.stringify**.JSON.stringify convert javascript object to a string following the JSON notation.
        3. **create a form to accept paramter as a post request.**
        package com.example.demo;

        import java.util.List;

        import java.util.ArrayList;

        import lombok.Data;

        @Data
        public class ParameterForm {
        List<String> paramList=new ArrayList<>();
        }

            4. **create a controller class for accepting the post request.**
            package com.example.demo;

            import java.util.List;

            import org.springframework.stereotype.Controller;
            import org.springframework.web.bind.annotation.RequestBody;
            import org.springframework.web.bind.annotation.RequestMapping;
            import org.springframework.web.bind.annotation.RequestMethod;
            import org.springframework.web.bind.annotation.ResponseBody;

            @Controller
            public class TestController {
            @RequestMapping(value="/home")
            public String checkParam()
            {
            return "test";  
            }
            @RequestMapping(value="/get",method = RequestMethod.POST)
            @ResponseBody
            public String getId(@RequestBody ParameterForm parameterForm)
            {
                List<String> paramList=parameterForm.getParamList();
                for(String param:paramList)
                {
                    System.out.println(param);
                }
                return "success";
            }
            }
    please focus on getId controller.
    you will find **@RequestBody ParameterForm parameterForm**.This form will accept the parameter which i send in ajax call. 

******************output**********************
1
2
3
4
5