POPBill Developers
SDK Reference
Java

Tutorial

Here is an example of parsing the POST request body received via Webhook as JSON in a SpringMVC development environment.

① To handle event message JSON, add the gson dependency information and update Maven.

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.3</version>
</dependency>

② Refer to the code below to add the function for handling the POST request body.
(This sample code assumes the callback URL is set to http(s)://webserverURL/pbconnect.)

import java.io.BufferedReader;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Controller
public class HomeController {

    @ResponseBody
    @RequestMapping(value = "pbconnect", method = RequestMethod.POST)
    public String webhook(HttpServletRequest request){

        StringBuffer strBuffer = new StringBuffer();
        String line = null;

        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
               strBuffer.append(line);
            }
        } catch (Exception e) {
            System.out.println("Error reading JSON string: " + e.toString());

            // Process the error message as a Return.
            // You can check this via POPBiLL Webhook initiation log
            return e.toString();
        }

        // Print Reqeust Body
        System.out.println(strBuffer.toString());

        // Parse the Request Body into JSON format
        // Refer to the bottom part of [Webhook message structure]
        // for the detailed items of the Request Body
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = (JsonObject)parser.parse(strBuffer.toString());
        System.out.println("eventType : " + jsonObject.get("eventType"));
        System.out.println("eventDT : " + jsonObject.get("eventDT"));

        // Compose JSON String to process the Webhook communication as ‘succeeded’
        return "{'result':'OK'}";
    }
}