Will be helpful the people who wants to learn restful services.
import java.util.List; import javax.ws.rs.DefaultValue; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.apache.log4j.Logger; import com.homedepot.ta.aa.log4j.SimpleExceptionLogger; import com.tcs.ResponseTO.GetTradeOptionsResponse; import com.tcs.ResponseTO.RegisterResponse; import com.tcs.ResponseTO.TradeOptionsTO; import com.tcs.managers.RegisterManager; import com.tcs.managers.SampleManager; import com.tcs.services.formatters.ExceptionXMLFormatter; import com.tcs.util.XMLGenerator; @Path("/ResponseService") public class ConvertResponseToXML { // Logger instance private static final Logger logger = Logger .getLogger(ConvertResponseToXML.class); /** * * @param version * @return * URL to hit this service: http://localhost:8081/XStreamExamples/rs/ResponseService/sampleResponse */ @POST @Produces("application/xml") @Path("/sampleResponse") public String myMethod(@DefaultValue("1") @QueryParam("version") int version) { if (logger.isDebugEnabled()) { logger.debug("Start myMethod"); logger.debug("version: '" + version + "'"); } String result = null; GetTradeOptionsResponse getTradeOptionsResponse = new GetTradeOptionsResponse(); SampleManager manager = null; ListtradeOptionList = null; try { switch (version) { case 1: if (logger.isDebugEnabled()) { logger.debug("in version 1"); } manager = new SampleManager(); tradeOptionList = manager.getTradeOptions(); getTradeOptionsResponse.setTradeOptionsTO(tradeOptionList); result = XMLGenerator.generateXML(getTradeOptionsResponse); break; default: throw new Exception("Unsupported version: " + version); } } catch (Exception e) { SimpleExceptionLogger.log(e, "XStreamExamples"); result = ExceptionXMLFormatter.formatMessage(e); } if (logger.isDebugEnabled()) { logger.debug("result: '" + result + "'"); logger.debug("End myMethod"); } return result; } /** * * @param version * @return * URL to hit this service: http://localhost:8081/XStreamExamples/rs/ResponseService/sampleResponse */ @POST @Produces("application/xml") @Path("/sampleResponse") public String response(@DefaultValue("1") @QueryParam("version") int version) { if (logger.isDebugEnabled()) { logger.debug("Start myMethod"); logger.debug("version: '" + version + "'"); } String result = null; RegisterResponse registerResponse; RegisterManager manager = new RegisterManager(); try { switch (version) { case 1: if (logger.isDebugEnabled()) { logger.debug("in version 1"); } registerResponse = manager.getCustomerDetails(); result = XMLGenerator.generateXML(registerResponse); break; default: throw new Exception("Unsupported version: " + version); } } catch (Exception e) { SimpleExceptionLogger.log(e, "XStreamExamples"); result = ExceptionXMLFormatter.formatMessage(e); } if (logger.isDebugEnabled()) { logger.debug("result: '" + result + "'"); logger.debug("End myMethod"); } return result; } }