UN MARSHALING XML to JAVA object for Restful Service using JAXRS

MagnusMZ

New Member
I am trying to achieve BOOLEAN return (true/false) based on XML file upload by user. For instance, I have a element direction which indicates type of data it contains. So i am interested in un marshaling the data and return boolean. Step 1: Interested in POST method and would be testing using POSTMAN chrome app.Step 2: Contents object to hold everything for un marshaling and marshalingpackage validator.service;\[code\]import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;// Created the Contents object to hold everything for un marshaling and marshaling@XmlRootElement( name = "contents" )public class Contents{ @XmlElement String portalarea; @XmlElement String portalsubarea; @XmlElement String direction; public String getportalarea() { return portalarea; } public String getportalsubarea() { return portalsubarea; } public String getdirection() { return direction; }}\[/code\]Step 3: Have Validation Class for receiving the Request and un marshal the XML to return boolean.\[code\]package validatorService;import java.io.InputStream;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBElement;import javax.xml.bind.Unmarshaller;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamReader;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;@Path ("/valid")public class ValidatorService{ boolean n_value = http://stackoverflow.com/questions/10538665/false; boolean r_value = false; @POST @Produces( MediaType.TEXT_PLAIN ) @Consumes("application/xml" ) public String validate( String xmlContent ) { HttpClient httpclient = new DefaultHttpClient(); try { if ( xmlContent != null ) { if ( xmlContent.startsWith( "https" ) ) { HttpGet xmlGet = new HttpGet( xmlContent ); HttpResponse response = httpclient.execute( xmlGet ); int responseStatus = response.getStatusLine().getStatusCode(); // String responseMessage = response.getStatusLine().getReasonPhrase(); if ( responseStatus == 200 ) { HttpEntity responseEntity = response.getEntity(); InputStream inStream = responseEntity.getContent(); Contents direction = unmarshalingContent( inStream, xmlContent ); if ( direction.equals( "N" ) ) { n_value = http://stackoverflow.com/questions/10538665/true; } else if ( direction.equals("R" ) ) { r_value = http://stackoverflow.com/questions/10538665/true; } else { System.out.println("Response Error : " + responseStatus ); // Should be // handled // properly } } else { System.out.println( " 'https' Format Error" ); // Should be handled properly } return "success"; } } catch ( Exception e ) { e.printStackTrace(); System.out.println( " Error caught at catch " + e ); // Should be handled properly for // all exception } finally { httpclient.getConnectionManager().shutdown(); } return null; } public Contents unmarshalingContent( InputStream inputStream, String resourceClass ) throws Exception { System.out.println( " welcome " ); if ( resourceClass == "xmlContent" ) { JAXBContext jc = JAXBContext.newInstance( "com.acme.bar" ); Unmarshaller u = jc.createUnmarshaller(); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader xReader = inputFactory.createXMLStreamReader( inputStream ); JAXBElement<Contents> jaxBElement = (JAXBElement<Contents>) u.unmarshal( xReader, Contents.class ); Contents portalArea = (Contents) jaxBElement.getValue(); Contents portalSubarea = (Contents) jaxBElement.getValue(); Contents direction = (Contents) jaxBElement.getValue(); return direction; } throw new Exception( "Invalid resource request" ); }}\[/code\]I am new to RESTful Service and i read few documents and based on instructions i am trying to achieve the given task. So any help, corrections, guidance, code is much appreciated.
 
Back
Top