CD part IX: Create the Webservices

  • 06/01/2017
  • 11 minuten leestijd

CD part IX: Create the Webservices

In this blog, we will be creating a simple SOAP-webservice which gets all accountstatus for the project.

create webservices

for this blog and example purposes, we will only be creating the AccountStatusWebservice.

generate default project

  1. navigate to the folder C:\projects
  2. create a new folder called easywebservices
  3. execute this maven command to generate a default structure
mvn archetype:generate -DgroupId=nl.com.devnl -DartifactId=AccountStatusWebservice -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

set needed dependencies in pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>
        <groupId>nl.com.devnl</groupId>
        <artifactId>AccountStatusWebservice</artifactId>
        <packaging>war</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>AccountStatusWebservice</name>

        <dependencies>

            <!-- our easy databse jar library -->
            <dependency>
                <groupId>nl.com.devnl</groupId>
                <artifactId>EasyDatabase</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>

            <!-- JAX-WS dependency -->
            <dependency>
                <groupId>com.sun.xml.ws</groupId>
                <artifactId>jaxws-rt</artifactId>
                <version>2.2.10</version>
            </dependency>    

            <!-- json dependency, needed to parse raw data -->
            <dependency>
                <groupId>com.googlecode.json-simple</groupId>
                <artifactId>json-simple</artifactId>
                <version>1.1.1</version>
            </dependency>
        </dependencies>

    </project>

create webservice

  1. when this project is generated, we have an App.java and a AppTest.java file, removed these, because we don't need them anymore.
  2. rename package to nl.com.devnl.entity
  3. in this package we create 2 entities AccountStatus & ListOfAccountStatus. These objects contain our data.

AccountStatus

    package nl.com.devnl.entity;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;
    import nl.com.devnl.dao.api.utils.Constants;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "AccountStatus", namespace = Constants.NAMESPACE_ENTITY)
    public class AccountStatus {

        @XmlElement(name = "id", namespace = Constants.NAMESPACE_ENTITY)
        private Integer id;
        @XmlElement(name = "name", namespace = Constants.NAMESPACE_ENTITY)
        private String name;

        /**
         * @return the id
         */
        public Integer getId() {
            return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(Integer id) {
            this.id = id;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
    }

ListOfAccountStatus

    package nl.com.devnl.entity;

    import java.util.List;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;
    import nl.com.devnl.dao.api.utils.Constants;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "ListOfAccountStatus", namespace = Constants.NAMESPACE_ENTITY)
    public class ListOfAccountStatus {

        @XmlElement(name = "items", namespace = Constants.NAMESPACE_ENTITY)
        private List<AccountStatus> items;

        public List<AccountStatus> getItems() {
            return items;
        }

        public void setItems(List<AccountStatus> items) {
            this.items = items;
        }
    }
  1. next we will be creating the Dao, which contains our operations on the data.

AccountStatusDao

    package nl.com.devnl.dao.api;

    import nl.com.devnl.entity.AccountStatus;
    import nl.com.devnl.entity.ListOfAccountStatus;

    public interface AccountStatusDao {

        boolean addAccountStatus(AccountStatus accountStatus);

        boolean updateAccountStatus(AccountStatus accountStatus);

        boolean deleteAccountStatus(int id);

        AccountStatus getAccountStatus(int id);

        ListOfAccountStatus getAllAccountStatus();
    }

AccountStatusDaoImpl

    package nl.com.devnl.dao.impl;

    import java.util.ArrayList;
    import java.util.List;
    import nl.com.devnl.dao.api.AccountStatusDao;
    import nl.com.devnl.db.EasyDatabase;
    import nl.com.devnl.db.EasyDatabaseImpl;
    import nl.com.devnl.entity.AccountStatus;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import nl.com.devnl.entity.ListOfAccountStatus;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;

    public class AccountStatusDaoImpl implements AccountStatusDao{

        @Override
        public boolean addAccountStatus(AccountStatus accountStatus) {
            EasyDatabase database = new EasyDatabaseImpl();
            return database.add(accountStatus);
        }

        @Override
        public boolean updateAccountStatus(AccountStatus accountStatus) {
            EasyDatabase database = new EasyDatabaseImpl();
            return database.update(accountStatus);
        }

        @Override
        public boolean deleteAccountStatus(int id) {
            EasyDatabase database = new EasyDatabaseImpl();
            return database.delete(AccountStatus.class.getSimpleName(), id);
        }

        @Override
        public AccountStatus getAccountStatus(int id) {
            EasyDatabase database = new EasyDatabaseImpl();
            String rawData = database.get(AccountStatus.class.getSimpleName(), id);
            return parseJson(rawData);
        }

        @Override
        public ListOfAccountStatus getAllAccountStatus() {
            EasyDatabase database = new EasyDatabaseImpl();
            List<String> rawData = database.getAll(AccountStatus.class.getSimpleName());
            return parseJson(rawData);
        }

        private AccountStatus parseJson(String rawJson){
            AccountStatus accountStatus = new AccountStatus();
            try {
                JSONObject jsonObject = (JSONObject)new JSONParser().parse(rawJson);
                accountStatus.setId(Integer.valueOf(jsonObject.get("id").toString()));
                accountStatus.setName((String)jsonObject.get("name"));
            } catch (ParseException ex) {
                Logger.getLogger(AccountStatusDao.class.getName()).log(Level.SEVERE, null, ex);
            }
            return accountStatus;
        }

        private ListOfAccountStatus parseJson(List<String> rawData){
            ListOfAccountStatus listOfAccountStatus = new ListOfAccountStatus();
            List<AccountStatus> accountStatusList = new ArrayList<>();
            for(String rawJson : rawData){
                try {
                    JSONObject jsonObject = (JSONObject)new JSONParser().parse(rawJson);
                    AccountStatus accountStatus = new AccountStatus();
                    accountStatus.setId(Integer.valueOf(jsonObject.get("id").toString()));
                    accountStatus.setName((String)jsonObject.get("name"));
                    accountStatusList.add(accountStatus);
                } catch (ParseException ex) {
                    Logger.getLogger(AccountStatusDao.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            listOfAccountStatus.setItems(accountStatusList);
            return listOfAccountStatus;
        }

    }

Constants

    package nl.com.devnl.dao.api.utils;

    public class Constants {
        //NAMESPACES
        public static final String NAMESPACE_QUESTION = "http://localhost:8071/nl/com/devnl/soap/request";
        public static final String NAMESPACE_ENTITY = "http://localhost:8071/nl/com/devnl/entity";
        public static final String NAMESPACE_WEBSERVICE = "http://localhost:8071/nl/com/devnl/webservice/api";
    }
  1. when we created the dao-layer, we will be creating the webservice

AccountStatusWebservice

    package nl.com.devnl.webservice.api;

    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebParam.Mode;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.ParameterStyle;
    import javax.jws.soap.SOAPBinding.Use;
    import nl.com.devnl.dao.api.utils.Constants;
    import nl.com.devnl.entity.AccountStatus;
    import nl.com.devnl.entity.ListOfAccountStatus;


    @WebService(name = "AccountStatusWebservice", serviceName = "AccountStatusWebservice", portName = "AccountStatusWebservicePort", targetNamespace = Constants.NAMESPACE_WEBSERVICE)
    @SOAPBinding(style = SOAPBinding.Style.RPC, use=Use.LITERAL, parameterStyle=ParameterStyle.WRAPPED)
    public interface AccountStatusWebservice {

        @WebMethod(operationName="addAccountStatus")
        @WebResult(name="addAccountStatusResponse", targetNamespace = Constants.NAMESPACE_WEBSERVICE)
        boolean addAccountStatus(@WebParam(name="accountStatus", targetNamespace = Constants.NAMESPACE_WEBSERVICE, mode=Mode.IN) AccountStatus accountStatus);

        @WebMethod(operationName="updateAccountStatus")
        @WebResult(name="updateAccountStatusResponse", targetNamespace = Constants.NAMESPACE_WEBSERVICE)
        boolean updateAccountStatus(@WebParam(name="accountStatus", targetNamespace = Constants.NAMESPACE_WEBSERVICE, mode=Mode.IN) AccountStatus accountStatus);

        @WebMethod(operationName="deleteAccountStatus")
        @WebResult(name="deleteAccountStatusResponse", targetNamespace = Constants.NAMESPACE_WEBSERVICE)
        boolean deleteAccountStatus(@WebParam(name="id", targetNamespace = Constants.NAMESPACE_WEBSERVICE, mode=Mode.IN) int id);

        @WebMethod(operationName="getAccountStatus")
        @WebResult(name="getAccountStatusResponse", targetNamespace = Constants.NAMESPACE_WEBSERVICE)
        AccountStatus getAccountStatus(@WebParam(name="id", targetNamespace = Constants.NAMESPACE_WEBSERVICE, mode=Mode.IN) int id);

        @WebMethod(operationName="getAllAccountStatus")
        @WebResult(name="getAllAccountStatusResponse", targetNamespace = Constants.NAMESPACE_WEBSERVICE)
        ListOfAccountStatus getAllAccountStatus();
    }

AccountStatusWebservice

    package nl.com.devnl.webservice.impl;

    import javax.jws.WebService;
    import nl.com.devnl.dao.api.AccountStatusDao;
    import nl.com.devnl.dao.impl.AccountStatusDaoImpl;
    import nl.com.devnl.entity.AccountStatus;
    import nl.com.devnl.entity.ListOfAccountStatus;
    import nl.com.devnl.webservice.api.AccountStatusWebservice;

    @WebService(endpointInterface = "nl.com.devnl.webservice.api.AccountStatusWebservice") 
    public class AccountStatusWebserviceImpl implements AccountStatusWebservice{

        @Override
        public boolean addAccountStatus(AccountStatus accountStatus) {
            AccountStatusDao dao = new AccountStatusDaoImpl();
            return dao.addAccountStatus(accountStatus);
        }

        @Override
        public boolean updateAccountStatus(AccountStatus accountStatus) {
            AccountStatusDao dao = new AccountStatusDaoImpl();
            return dao.updateAccountStatus(accountStatus);
        } 

        @Override
        public boolean deleteAccountStatus(int id) {
            AccountStatusDao dao = new AccountStatusDaoImpl();
            return dao.deleteAccountStatus(id);
        }

        @Override
        public AccountStatus getAccountStatus(int id) {
            AccountStatusDao dao = new AccountStatusDaoImpl();
            return dao.getAccountStatus(id);
        }

        @Override
        public ListOfAccountStatus getAllAccountStatus() {
            AccountStatusDao dao = new AccountStatusDaoImpl();
            return dao.getAllAccountStatus();
        }
    }
  1. we have create our webservice, lets build this project
  2. navigate to C:\projects\easywebservices\AccountStatusWebservice
  3. execute mvn clean install
  4. rename the war in the target directory to accountStatusWebservice
  5. copy the war to C:\tools\tomcatwebserviced\webapps
  6. start tomcat server for the webservice by executing tomcat ws develop - start.cmd
  7. when everything is done the service is available on http://localhost:8071/accountStatusWebservice/accountstatus?wsdl