Skip to main content

Posts

Showing posts from 2013

Transform Object into XML using JAXB (Part 2)

In one of the posts  we had seen how to convert Java objects into XML. This was done using JAXB and XSD .  What if you don't like to write XSD by hand. Well, you can use any online or offline tools available, to generate the desired XSD from XML. Most of the times the generated XSD will be nearly accurate and you have to hand edit them to make it 100% perfect. Alright. What if you don't know XSD at all. Is there a simpler way? Yes there is. Here we are going to use a different approach to achieve exactly the same result that we have got in the  old  post. Open Netbeans and create a Java Application File->New Project->Java Application Enter "XMLBinding2" in the Project Name field Click Finish Create the following Java classes Replace the Java files with the following contents appropriately. package xmlbinding2; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper

Dynamic SOAP Service Client

If you have written SOAP service client, you might know that you need the WSDL file; need to generate Java code for that,compile that Java classes and add it as dependency for your module. What would you do if you have to incorporate your code with a new SOAP service every now and then? What would you do if all you need is to consume the service and do a little processing on the output, i.e., you need the data in XML format? What would you do if you don't have a complete WSDL? What would you do if your service is in .NET whose WSDL is having problem while generating Java classes? Is there a way to write a dynamic client which can consume any SOAP service? .... YES!... there is a way. Let's quickly write a web (SOAP) service. Software used: Java 7 NetBeans IDE 7.4 GlassFish 4.0 Maven Create a web project and choose Glassfish as server. Now add web service (not a rest service) as below. Edit the SimpleService.java as follows. package com.mycom

Permutaion Program

Let's see how we can apply permutation on the following input. a, b,c The expected output is, a b c a c b b c a b a c c a b c b a The logic is simple. Each position p (where p>1) will have p rotations after which the next position is rotated. input: a b c position: 3 2 1 print (a, b, c) rotate b   and print (a, c, b) rotate c ; (a, b, c) count exhausted. position 2 is already rotated 2 times rotate next position which is a and print (b, c, a) rotate c and print (b, a, c) rotate a ; (b, c, a) count exhausted. position 2 is already rotated 2 times rotate next position which is b and print (c, a, b) rotate a and print (c, b, a) rotate b ; (c, a, b) count exhausted. position 2 is already rotated 2 times try rotating next position which is c ;  count is exhausted. position 3 is already rotated 3 times. look for next position no more positions, hence terminate public class Permutation {     public static void main(String[] args) {         L

REST Service on JBoss 7 (using CXF) - Part 4

In the last three posts we have been seeing how to host a RESTful service on JBoss 7 by defaulting to the container implementation of JAX-RS, i.e., RESTEasy . However you can use any other implementation with JBoss 7. In fact JBoss 7 itself uses RESTEasy as JAX-RS (RESTful service) implementation and CXF as JAX-WS (SOAP service) implementation. But what you will miss if you use other implementations such as CXF or other containers such as Spring , is the container management/injection. That is, you can inject any resource such as CDI bean, EJB, Persistence context, transaction, etc into our LibraryService (using annotation/xml configuration) that we saw in the last post . Where as you need to lookup them manually if you use CXF or use any other dependency injection provider such as Spring. Generally it is less hassle to leave it to the app server to manage these resources; but if you dont have a choice (when other teams are using different library or the feature you need is not sup

REST Service on JBoss 7 (using RESTEasy) - Part 3

In the last post we learnt how to output JSON from a REST service. Here is the sample JSON output that we got in the last post. [     {         "name": "Java The Complete Reference, 8th Edition",         "author": "Herbert Schildt",         "price": 479     },     {         "name": "Jboss As 7 Configuration, Deployment, And Administration",         "author": "Francesco Marchioni",         "price": 450     },     {         "name": "RESTful Java with JAX-RS 2.0 2ed",         "author": "Bill Burke",         "price": 2018     } ] RESTEasy (default JAX-RS provider for JBoss AS 7) uses Jackson as the JSON provider. What if your clients are using Jettison for parsing the response from your REST service? Hold on. What is the difference between Jackson and Jettison ? Here are a few differences. Jettison relies on JAXB annotation (such

REST Service on JBoss 7 (using RESTEasy) - Part 2

In this post we are going to enhance our REST service (which we developed in the last post) to return a user defined object instead of java.lang.String or any other primitive type. 1. Create a class called Book and paste the following contents into it. package org.jbosstest.jbosstest; import java.io.Serializable; public class Book implements Serializable {     private String name;     private String author;     private float price;     public Book() {     }     public Book(String name, String author, float price) {         this.name = name;         this.author = author;         this.price = price;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public String getAuthor() {         return author;     }     public void setAuthor(String author) {         this.author = author;     }     public float getPrice() {         return price;     }     public void setPrice(float price) {       

REST Service on JBoss 7 (using RESTEasy) - Part 1

In this section we are going to see how we can host a simple REST service on JBoss AS 7.2 1. Start NetBeans 7.3.1 and create a new maven-web project as follows. 2. Name it as JbossTest. 3.  Choose JBoss as the server. 4. Click Finish 5. You will see the following under Projects tab 6. Create a class named TestService and copy paste the content from below. package org.jbosstest.jbosstest; import javax.ejb.LocalBean; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @LocalBean @Path("/test") public class TestService {     @GET     @Path("hello/{name}")     public String sayHello(@PathParam("name") String name) {         return "Hello " + name;     } } 7. Create a class called ApplicationConfig and paste the following content. This is to register the class TestService as a RESTService. package org.jbosstest.jbosstest; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Applic

Registering JBoss AS 7 with Netbeans

Here we are going to register JBoss AS 7 (7.2.0.Final in particular) with NetBeans 7.3.1 or better. Download netbeans 7.3.1 from here . Choose Java EE version. Install JBoss AS 7.2 from the previous post   Start netbeans Go to services tab  Right click Servers and select Add Server . Select JBoss  Select the jboss installation location. Click Next and Finish After the successful registration, you will see a jboss server entry under the Servers node. Right click that and choose Start