JAX-RS on Weblogic using Jersey

In my current project, I have a RESTful WebService. Because we are using Weblogic 10.3 with JEE5, I have to use Jersey. Due to the requirement, I used XML instead of JSON. Also, I’d like to focus on JAX-RS client side using Jersey. Server side is relatively simple and lots of tutorials are available.

  • Weblogic 10.3
  • IntelliJ
  • Jersey 1.12
  • Xerces 2.11

[XML binding with POJO – using JAXBElement]

Message.java

public class Message
{
//Asset, Related ID, Functional Area, Message Type, Received At
public String asset;
public String relatedID;
public String functionalArea;
public String messageType;
public String receivedAt;

@Override
public String toString()
{
return "Message{" +
"asset='" + asset + '\'' +
", relatedID='" + relatedID + '\'' +
", functionalArea='" + functionalArea + '\'' +
", messageType='" + messageType + '\'' +
", receivedAt='" + receivedAt + '\'' +
'}';
}
}

Simple object without any complex type. Easy to parse.

@Path("/nprf")
public class NprfResource......(obmitted)...

  @GET
  @Path("/testXML")
  @Produces(MediaType.APPLICATION_XML)
  public JAXBElement<Message> testXML()
  {
    Message message = new Message();
    message.asset = "test";
    message.functionalArea = "myarea";
    message.messageType = "testtype";
    message.receivedAt = "January 1";
    message.relatedID = "000";

    return new JAXBElement( new QName( "testXML" ), Message.class, message );
  }

Defining return type as JAXBElement. It will be useful if the returned object type is complex object and don’t want to add XmlRootElement tag all over.

 try
    {
      // RESTful WebService Client call. Optional to create a DefaultClientConfig.
      DefaultClientConfig config = new DefaultClientConfig();
      config.getProperties().put( ClientConfig.FEATURE_DISABLE_XML_SECURITY, 
       "true" );
      Client client = Client.create( config );
      WebResource webResource 
       = client.resource( "http://localhost:30020/CMWeb/rest/nprf" );

      GenericType<JAXBElement> messageType = new GenericType<JAXBElement>()
      {
      };
      Message message = webResource.path( "testXML" )
                        .accept( MediaType.APPLICATION_XML_TYPE )
                        .get( messageType ).getValue();
      System.out.println( "### " + message );

    }
    catch ( Exception e )
    {
      log.safeError( "testTestXML", e.getClass().getName(), e );
      fail( e.getMessage() );
    }

I was getting “org.xml.sax.SAXNotRecognizedException: http://javax.xml.XMLConstants/feature/secure-processing&#8221; exception when I tried to parse XML to Java object. To resolve the issue, I had to replace Xerces with newer version. I think somewhere in the environment uses the old version of Xerces.

From URL result

  <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes” ?>
<testXML>
  <asset>test</asset>
  <relatedID>000</relatedID>
  <functionalArea>myarea</functionalArea>
  <messageType>testtype</messageType>
  <receivedAt>January 1</receivedAt>
  </testXML>

Test Result below.

About to run testTestXML
-----------------------------------------------------------------
### Message{asset='test', 
relatedID='000', 
functionalArea='myarea', 
messageType='testtype', 
receivedAt='January 1'}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s