Creating a simple web service using JAX-WS and JAXB

  1. Client provides Customer Name as input.
  2. Web Service built is going to take the input from the client and retrieve the account details like account number, balance and returns along with the customer name in the response. (Note: We are trying to play with a simple service and so values are hard-coded for the account details).
  3. Using JAXB we are going to change the default order of the properties in the response.
package com.somecompany;
public class Account {
private int returns;
private int balance;
private String accountNumber;
private String customerName;

public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public int getReturns() {
return returns;
}
public void setReturns(int returns) {
this.returns = returns;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
package com.somecompany;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "AccountWebService")
public class AccountWebService {
@WebMethod(operationName = "getAccountDetails")
public Account getAccountDetails(@WebParam(name = "name") String customerName) {
Account account = new Account();
account.setAccountNumber("12345");
account.setBalance(10000);
account.setReturns(500);
account.setCustomerName(customerName);
return account;
}
}
Thu Feb 08 18:52:48 CST 2018 : Security manager installed using the Basic server security policy.Thu Feb 08 18:52:48 CST 2018 : Apache Derby Network Server - 10.11.1.2 - (1629631) started and ready to accept connections on port 1527
import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="CustomerAccount")@XmlType(propOrder = {"customerName","returns", "balance", "accountNumber"})
@XmlElement(name="AccountNumber")public String getAccountNumber() {return accountNumber;}@XmlElement(name="Balance")public int getBalance() {return balance;}
@XmlElement(name="Returns")public int getReturns() {return returns;}@XmlElement(name="CustomerName")public String getCustomerName() {return customerName;}
package com.somecompany;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlType; @XmlRootElement(name="CustomerAccount")@XmlType(propOrder = {"customerName","returns", "balance", "accountNumber"})public class Account {private int returns;private int balance;private String accountNumber;private String customerName; @XmlElement(name="AccountNumber")public String getAccountNumber() {return accountNumber;}public void setAccountNumber(String accountNumber) {this.accountNumber = accountNumber;}@XmlElement(name="Balance")public int getBalance() {return balance;}public void setBalance(int balance) {this.balance = balance;}@XmlElement(name="Returns")public int getReturns() {return returns;}public void setReturns(int returns) {this.returns = returns;}@XmlElement(name="CustomerName")public String getCustomerName() {return customerName;}public void setCustomerName(String customerName) {this.customerName = customerName;}}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akkiraju Ivaturi

Akkiraju Ivaturi

I am a technology enthusiast interested in Software Integrations. Always excited to help and learn new technologies, facts in life :)