如何使用curl测试弹簧控制器

时间:2020-02-04 08:38:24

标签: spring-mvc curl

Good Day Guy,我正在开发Web服务。 我创建了一个名为login的控制器

我要测试此控制器是否正在工作或使用curl返回某些东西。 但我不知道语法或如何测试。 这是代码。 devTest是项目文件夹的名称。 这是在包app.controller下。

请帮助我。预先感谢

@RequestMapping(value = "login", method = RequestMethod.POST, headers = {"Content-type=application/json"})
public @ResponseBody Map getResponse(@RequestBody Map request) {Map map = new HashMap<String, String>();
        public @ResponseBody Map getResponse(@RequestBody Map request) {Map map = new HashMap<String, String>();
    String username = "";
    String password = "";
    String fullname = "";
    String imei = "";
    int cifid = 0;
    int accounts = 0;
    String firstAccount="";
    try {
        username = String.valueOf(request.get("username"));
        password = String.valueOf(request.get("password"));
        //imei = String.valueOf(request.get("imei")); 

        AuthenticObj auth = authentication.authenticate(username, password);
        if(auth == null) {
            map.put("success", false);
            map.put("message", "Invalid login.");
            return map;
        }
        if(!password.toLowerCase().equals(auth.getPassword().toLowerCase())) {
            map.put("success", false);
            map.put("message", "Invalid login.");
            return map;
        }

        cifid = auth.getCifid();
        firstAccount= accountManager.getFirstAccount(cifid);
        fullname = auth.getFullname();

    }catch(Exception e) {

        logger.error("ERROR", e);
        map.put("success", false);
        map.put("message", "An error occured. Response not processed.");
        return map;
    }

    map.put("cifID", cifid);
    map.put("firstAccount", firstAccount);
    map.put("success", true);
    map.put("message", "Response Received.");
    return map;

}

1 个答案:

答案 0 :(得分:0)

不确定为什么在问题中提到项目文件夹和Java包名称。

要测试curl,您需要启动服务器并向您的应用程序发出http请求(通常是从浏览器中执行)。下面是示例curl命令

structure(list(Date = structure(1:7, .Label = c("2-17", "2-18", 
"2-19", "2-20", "2-21", "2-22", "2-23"), class = "factor"), CC = list(
    structure(c(0.0292, 0.1803, 0.1063, 0.3555, 0.0227, 0.0471, 
    0.2588, 1, 2.92, 18.03, 10.63, 35.55, 2.27, 4.71, 25.88, 
    100), .Dim = c(8L, 2L), .Dimnames = list(c("Unique to Jsc             ", 
    "Unique to Uoc             ", "Unique to FF              ", 
    "Common to Jsc, and Uoc    ", "Common to Jsc, and FF     ", 
    "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
    "Total                     "), c("Coefficient", "    % Total"
    ))), structure(c(0.0414, 0.2307, 0.1813, 0.3428, 0.0408, 
    0.0137, 0.1494, 1, 4.14, 23.07, 18.13, 34.28, 4.08, 1.37, 
    14.94, 100), .Dim = c(8L, 2L), .Dimnames = list(c("Unique to Jsc             ", 
    "Unique to Uoc             ", "Unique to FF              ", 
    "Common to Jsc, and Uoc    ", "Common to Jsc, and FF     ", 
    "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
    "Total                     "), c("Coefficient", "    % Total"
    ))), structure(c(0.0348, 0.2298, 0.4534, 0.0453, 0.1252, 
    0.0406, 0.0709, 1, 3.48, 22.98, 45.34, 4.53, 12.52, 4.06, 
    7.09, 100), .Dim = c(8L, 2L), .Dimnames = list(c("Unique to Jsc             ", 
    "Unique to Uoc             ", "Unique to FF              ", 
    "Common to Jsc, and Uoc    ", "Common to Jsc, and FF     ", 
    "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
    "Total                     "), c("Coefficient", "    % Total"
    ))), structure(c(0.0693, 0.1241, 0.3104, 0.0885, 0.1706, 
    0.0563, 0.1807, 1, 6.93, 12.41, 31.04, 8.85, 17.06, 5.63, 
    18.07, 100), .Dim = c(8L, 2L), .Dimnames = list(c("Unique to Jsc             ", 
    "Unique to Uoc             ", "Unique to FF              ", 
    "Common to Jsc, and Uoc    ", "Common to Jsc, and FF     ", 
    "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
    "Total                     "), c("Coefficient", "    % Total"
    ))), structure(c(0.0431, 0.1621, 0.3896, 0.2243, 0.1005, 
    -0.0339, 0.1143, 1, 4.31, 16.21, 38.96, 22.43, 10.05, -3.39, 
    11.43, 100), .Dim = c(8L, 2L), .Dimnames = list(c("Unique to Jsc             ", 
    "Unique to Uoc             ", "Unique to FF              ", 
    "Common to Jsc, and Uoc    ", "Common to Jsc, and FF     ", 
    "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
    "Total                     "), c("Coefficient", "    % Total"
    ))), structure(c(0.0656, 0.1021, 0.2638, 0.1299, 0.2102, 
    0.0061, 0.2223, 1, 6.56, 10.21, 26.38, 12.99, 21.02, 0.61, 
    22.23, 100), .Dim = c(8L, 2L), .Dimnames = list(c("Unique to Jsc             ", 
    "Unique to Uoc             ", "Unique to FF              ", 
    "Common to Jsc, and Uoc    ", "Common to Jsc, and FF     ", 
    "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
    "Total                     "), c("Coefficient", "    % Total"
    ))), structure(c(0.0802, 0.1114, 0.6962, 0.0845, 0.1544, 
    -0.0958, -0.0309, 1, 8.02, 11.14, 69.62, 8.45, 15.44, -9.58, 
    -3.09, 100), .Dim = c(8L, 2L), .Dimnames = list(c("Unique to Jsc             ", 
    "Unique to Uoc             ", "Unique to FF              ", 
    "Common to Jsc, and Uoc    ", "Common to Jsc, and FF     ", 
    "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
    "Total                     "), c("Coefficient", "    % Total"
    )))), Variance_Type = list(c("Unique to Jsc             ", 
"Unique to Uoc             ", "Unique to FF              ", "Common to Jsc, and Uoc    ", 
"Common to Jsc, and FF     ", "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
"Total                     "), c("Unique to Jsc             ", 
"Unique to Uoc             ", "Unique to FF              ", "Common to Jsc, and Uoc    ", 
"Common to Jsc, and FF     ", "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
"Total                     "), c("Unique to Jsc             ", 
"Unique to Uoc             ", "Unique to FF              ", "Common to Jsc, and Uoc    ", 
"Common to Jsc, and FF     ", "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
"Total                     "), c("Unique to Jsc             ", 
"Unique to Uoc             ", "Unique to FF              ", "Common to Jsc, and Uoc    ", 
"Common to Jsc, and FF     ", "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
"Total                     "), c("Unique to Jsc             ", 
"Unique to Uoc             ", "Unique to FF              ", "Common to Jsc, and Uoc    ", 
"Common to Jsc, and FF     ", "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
"Total                     "), c("Unique to Jsc             ", 
"Unique to Uoc             ", "Unique to FF              ", "Common to Jsc, and Uoc    ", 
"Common to Jsc, and FF     ", "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
"Total                     "), c("Unique to Jsc             ", 
"Unique to Uoc             ", "Unique to FF              ", "Common to Jsc, and Uoc    ", 
"Common to Jsc, and FF     ", "Common to Uoc, and FF     ", "Common to Jsc, Uoc, and FF", 
"Total                     "))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L))

您可以看到其他卷曲示例here