Skip to main content

CXF-RS: Java Code Generation from xml/xsd

Cleaner approach is to use xjc like this:



Continue to dirty approach below if u need to:

When working with CXF-RS services often we start with XML message that will be served. After that we go through endless iteration of generating the right Java POJO's for the XML. The library like apache XMLBeans generate the Java code but the code generated is not what a developer would write and generally tedious to modify and understand.

I have come across a work around for generating Java classes from XSD (xml) which is very close to what a developer with decent experience will write. 

Follow the steps given below to explore the solution:
1. Generate the wsdl from from xsd as given in at this link (http://cxf.apache.org/docs/xsd-to-wsdl.html).
2. The Java classes can be generated from wsdl from maven plugin entry below find complete details here http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html


 org.apache.cxf
 cxf-codegen-plugin
 ${cxf.version}
 
  
   generate-sources
   generate-sources
   
    ${project.build.directory}/generated/cxf
    
     
      ${basedir}/src/main/wsdl/myService.wsdl
     
    
   
   
    wsdl2java
   
  
 


Hope this reduce your headache. Happy coding :)

Comments

Popular posts from this blog

Write a Java program to read console data using bufferedreader API

BufferedReader is a class which simplifies reading text from a character input stream. It buffers the characters in order to enable efficient reading of text data. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example, // BufferedReader in = new BufferedReader(new FileReader("foo.in")); will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream

java.net.SocketException: Connection reset

I am able to fix the problem via setting the following params on the HTTPClient class... client.getParams().setParameter("http.socket.timeout", new Integer(0)); client.getParams().setParameter("http.connection.stalecheck", new Boolean(true)); java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.io.BufferedInputStream.fill(Unknown Source) at java.io.BufferedInputStream.read(Unknown Source) at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77) at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105) at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1115) at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1832) at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590) at org.apache.commons.httpclient.HttpMethodBase.execute(

Hide empty last column from Table/Tree Viewer

1. How to remove last vestigial column from TableViewer You might have seen that there is dummy or vestigial column in the TableViewer (as marked by the red oval in the below figure) which  is annoying at times because not only it look ugly but more so it occupy;s the precious  real state from your component/control like shown in figure below: Solution: You can use  TableColumnLayout from  org.eclipse.jface.layout package inside  org.eclipse.jface plugin which allows to layout the columns in your TableViewer in essentially two ways: Firstly via mentioning the width in pixel for each column present in the Viewer  Secondly, we could mentioned weights The data could be supplied to the layout via of the implementation of  ColumnLayoutData. The following snippet shows how to achieve it: tableColumnLayout.setColumnData(tableColumn, new ColumnWeightData(1)); Explanation of the above line: tableColumnLayout is   instance of  ColumnLayoutData tableColumn is the column on which