【问题标题】:How to pass multiple ids in API Get method Endpoint using Spring Boot如何使用 Spring Boot 在 API Get 方法 Endpoint 中传递多个 id
【发布时间】:2018-06-01 08:33:56
【问题描述】:

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

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

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

这是否可以使用 Spring Boot 和 JDBC。

【问题讨论】:

标签: java spring-boot spring-jdbc


【解决方案1】:

应该这样做。

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

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

【讨论】:

  • 感谢您的建议,但我将如何在 SQL 查询中传递 id。我的意思是对于单个员工的详细信息,我只是触发“jdbcTemplate.query("SELECT * FROM device_registration WHERE Id= ?", new DeviceRowMapper(),id);"" 但是对于传递多个 id,我必须更改查询也是。另外我将如何在端点中传递 id。我的意思是“/employeeList/{1,2,3}”。
【解决方案2】:
                    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-29
    • 2018-04-20
    • 2022-10-04
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    相关资源
    最近更新 更多