Announcement Announcement Module
Collapse
No announcement yet.
WebService and exported JPA method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • WebService and exported JPA method

    Hi!

    I'm creating the following WebService on the dmServer:

    Code:
    package insertPointWS.ws;
    
    //Libraries for Spring-WS part
    import insertPointWS.Positions;
    
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.Namespace;
    import org.jdom.xpath.XPath;
    
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;
    
    
    public class InsertPointEndpoint
      extends AbstractJDomPayloadEndpoint
      implements InitializingBean {
    	
    	private Namespace namespace;
    	private XPath latXPath;
    	private XPath lonXPath;
    	private XPath fleetidXPath;
    	
    	@Autowired
    	private Positions positions;
    	
    	private float lat;
    	private float lon;
    	private long fleetid;
    
    
    	protected Element invokeInternal(Element element)
    	throws Exception {
    
    		String response = "OKEY";
    		
    		//Extract Position from Request
    		extractPositionFromRequest(element);
    		
    		//If code_error OKEY
    		if(!CheckErrorCondition()){
    			//Insert or Update position into DB using business logic
    			boolean ok = positions.insertPosition(lat, lon, fleetid);
    			if(!ok)
    				response="ERROR";
    		}
    		else{
    			response="ERROR";
    		}	
    			
    		//Create and return response
    		return createResponse(response);
    	}
    	
    	private void extractPositionFromRequest(Element element)
    	throws JDOMException {
    		
    		//Get information from XML Request
    		//if Latitude/Longitude is NULL assign a value Out of Bound
    		this.lat = Float.parseFloat(latXPath.valueOf(element).length()==0?"999":latXPath.valueOf(element));
    		this.lon = Float.parseFloat(lonXPath.valueOf(element).length()==0?"999":lonXPath.valueOf(element));
    		this.fleetid = Long.parseLong(fleetidXPath.valueOf(element).length()==0?"999":fleetidXPath.valueOf(element)); 			
    	}
    	
    	public void afterPropertiesSet() throws Exception {
    		namespace = Namespace.getNamespace("ip", "http://myflit.com/ip");
    
    		latXPath = XPath.newInstance("//ip:Lat");
    		latXPath.addNamespace(namespace);
    		
    		lonXPath = XPath.newInstance("//ip:Lon");
    		lonXPath.addNamespace(namespace);
    		
    		fleetidXPath = XPath.newInstance("//ip:FleetId");
    		fleetidXPath.addNamespace(namespace);
    	}
    	
    	private Element createResponse(String response) {
    		Element responseElement = new Element("InsertPointResponse", namespace);
    		//Creates XML response		
    		responseElement.addContent(new Element("Response", namespace).setText(response));
    		return responseElement;
    	}
    	
    	private boolean CheckErrorCondition(){
    		boolean error = false;
    		//Latitude or Longitude Out of Bounds		
    		if(this.lat>90||this.lat<-90||this.lon>180||this.lon<-180)
    			error=true;
    		return error;
    	}
    }
    The WebService is not working properly and it's returning this has a response:

    Code:
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Header/>
       <SOAP-ENV:Body>
          <SOAP-ENV:Fault>
             <faultcode>SOAP-ENV:Server</faultcode>
             <faultstring xml:lang="en">java.lang.NullPointerException</faultstring>
          </SOAP-ENV:Fault>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    The point is that when commenting this line:
    positions.insertPosition(lat, lon, fleetid);
    in the web service code, it works correctly.
    So I can guess there is a problem when using this service (exported to the OSGi service register by the bundle insertPointWS.jpa) with my WebServices.

    Is there any incompatibility between WebServices and this OSGi service register? What am I doing wrong?

    Thank you very much!

    Best regards,
    Victor

  • #2
    My guess would be that the Positions field in the class that you've annotated with @Autowired hasn't been initialised. There could be a couple of reasons for this:

    1) the Autowired annotation couldn't be loaded
    2) annotation-config isn't enabled.

    For 1, it's worth checking that your bundle imports the org.springframework.beans.factory.annotation package. For2, it's worth checking that you have <context:annotation-config/> specified in your application context.

    You might also like to try injecting Positions into InsertPointEndpoint's constructor, using a <constructor-arg> rather than @Autowired, just to check that it works in that scenario.

    Comment


    • #3
      Hi Andy!

      Thank you for your help.
      My bundle imports org.springframework.beans.factory.annotation package and I added <context:annotation-config /> to my module-context.xml file inside the directory src/main/resources/META-INF/spring.

      I tried to add the constructor-arg to the spring-ws-servlet.xml file in the directory src/main/resources/MODULE-INF/WEB-INF where I defined the bean:
      Code:
      	<bean id="InsertPointEndpoint" class="insertPointWS.ws.InsertPointEndpoint">
      	</bean>
      but the problem is that I don't know how to do it without the @Autowired because the "positions" service is referenced using the OSGi service register. If I do something like that:

      Code:
      	<bean id="InsertPointEndpoint" class="insertPointWS.ws.InsertPointEndpoint">
                     <constructor-arg ref="positions" />
      	</bean>
      when compiling it complains with:

      1 constructor arguments specified but no matching constructor found in bean 'InsertPointEndpoint'

      How can I inject Positions using a <constructor-arg> instead of @Autowired but referencing a service in the OSGi register?

      The service was exported by the bundle myflit.jpa using the osgi-context.xml file like that:

      Code:
              <osgi:service ref="positions" interface="myflit.Positions" />
      Thank you again!

      Regards,
      Victor

      Comment


      • #4
        You can use the <osgi:reference> tag to consume a service. There's more information about this in the Spring DM documentation

        Comment


        • #5
          Hi Andy!

          Thank you for your help.
          I was making a silly mistake, I defined the bean 'InsertPointEndpoint' in the wrong place, I update the svn in sourceforge with the correct code, just in case some one want to use it as an example of a webservice running on dmServer.

          Comment

          Working...
          X