【问题标题】:How to send parameter from form by json to controller in SpringSpring中如何通过json从表单向控制器发送参数
【发布时间】:2021-04-23 16:37:41
【问题描述】:

我正在使用 spring 和 web 服务通过 json 数据提交和拨打带有电话号码的电话,我想获取以下 URL:

https://myservice.com/oapi/v1/call/click-to-call/phoneNumber 

phoneNumber 是任意号码,用户点击这个电话号码会自动从 json 调用

这是我的 JSP

<form action="https://myservice.com/oapi/v1/call/click-to-call/${orderDetail.phoneNumber}" method="POST">
    <input type="submit" name="phoneNumber" value="${orderDetail.phoneNumber}" />
</form>

这是我的课: (更新

@RequestMapping(value="https://myservice.com/oapi/v1/call/click-to-call/{phoneNumber}", method = RequestMethod.POST)
public String clickToCall(HttpServletRequest request, @PathVariable("phoneNumber") String phoneNumber) {

logger.debug("Phone number is calling: " + phoneNumber);
phoneNumber = request.getParameter(phoneNumber);

// String url = "https://myservice.com/oapi/v1/call/click-to-call/0902032618"; 

    try {
        URL obj = new URL("https://myservice.com/oapi/v1/call/click-to-call/" + phoneNumber);

        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
        String encoded = Base64.getEncoder().encodeToString((AppConstants.SIPUSERNAME+":"+AppConstants.SIPPASSWORD).getBytes(StandardCharsets.UTF_8));  //Java 8
        // con.setRequestProperty("Authorization", "Basic "+encoded);

        System.out.println("Original String is " + encoded);

        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");                 // httpUrlConn
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Accept-Language", "UTF-8");
        con.setRequestProperty("x-auth-app-id", "61666411659356156");
        con.setRequestProperty("x-auth-app-hash", "a44f4ea21475fa6761392ba4bc659994330bee771c413b2c207490a79f9ec78c2a6");

        String urlParameters = "{\"sipUser\":\"vchi_cc\",\"sipPassword\" : \"m9Bp7s+CtQj85HygnIFjPn7O4Vithrun\"}";

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);

        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();                       
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());
    }
    catch (ConnectException ce) {
    ce.printStackTrace();
    System.out.println("Our server connection timed out");
  }

  catch (Exception e) {
       e.printStackTrace();
       System.out.println("https request error:{}");
  }

  return "orderDetailPop";

}

当我点击电话号码时,会发生以下异常和错误:

{"success":false,"message":"APPID_MISSING"}
        

好像提交的时候,我的类还没有从表单中调用,如何解决这个问题?

【问题讨论】:

    标签: java ajax spring spring-boot spring-mvc


    【解决方案1】:

    在春季,@RequestParam 是一个添加到请求中的参数,例如 ?phoneNumber=...

    如果您希望它成为 URL 的一部分,那么您需要使用 @PathVariable 并将参数包含在 @RequestMapping 的值中

    那么您的方法将如下所示:

    @RequestMapping(value="https://myservice.com/oapi/v1/call/click-to-call/{phoneNumber}", method = RequestMethod.POST)
    public String clickToCall(HttpServletRequest request, @PathVariable("phoneNumber") String phoneNumber) {
    

    【讨论】:

    • 我会在你的支持下尝试^^
    • 嗨@Veselin Davidov:我已经更新了代码并用PathVariable替换了RequestParam,但同样的错误
    猜你喜欢
    • 1970-01-01
    • 2014-03-07
    • 2018-11-08
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2020-08-23
    • 2011-07-03
    • 2014-04-26
    相关资源
    最近更新 更多