luongkhanh
luongkhanh

Reputation: 1813

How to send parameter from form by json to controller in Spring

I'm using spring and web service to submit and call phone with phone number via json data, I want to get URL with following as:

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

with phoneNumber is any number, and user click this phone number will auto call from json

This is my 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>

This is my class: (UPDATED)

@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";

}

When I click phone number is happent an exception and error following as:

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

it seem when submit, my class is not yet called from form, How to fix the problem ?

Upvotes: 0

Views: 86

Answers (1)

Veselin Davidov
Veselin Davidov

Reputation: 7071

In spring the @RequestParam is a parameter that is added to the request like ?phoneNumber=...

If you want it to be part of the URL then you need to use @PathVariable and also include the parameter in the value of the @RequestMapping

Then your method will look something like:

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

Upvotes: 1

Related Questions