简体   繁体   中英

Simple nusoap request returns java.lang.nullPointerException

I'm trying to consume a Web Service published by JAX-WS, which I have never done before so please bear with me. When performing the nusoap call in my local environment, it provides all data requested. However, once I deploy the java app to my Dev environment, it returns a null pointer exception from the server side.

Below are snippets of my code (would be glad to provide with any further info you may need, since i've never used JAX WS before). Thanks in advance!!!

XML file, published in the example url below (relevant sections)

http://services.myapp.dev.com:8080/myapp-1.0-SNAPSHOT/MyService?wsdl

/* Content */

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.7-b01  svn-revision#${svn.Last.Changed.Rev}. 
-->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.7-b01  svn-revision#${svn.Last.Changed.Rev}. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.myapp.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.myapp.com/" name="MyServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.myapp.com/" schemaLocation="http://services.myapp.com:8080/myapp-1.0-SNAPSHOT/MyService?xsd=1"/>
</xsd:schema>
</types>
<message name="SearchDailyRecords">
<part name="parameters" element="tns:SearchDailyRecords"/>
</message>
<message name="SearchDailyRecordsResponse">
<part name="parameters" element="tns:SearchDailyRecordsResponse"/>
</message>
<portType name="MyService">
<operation name="SearchDailyRecords">
<input wsam:Action="http://services.myapp.com:8080/myapp-1.0-SNAPSHOT/MyService/SearchDailyRecordsRequest" message="tns:SearchDailyRecords"/>
<output wsam:Action="http://services.myapp.com:8080/myapp-1.0-SNAPSHOT/MyService/SearchDailyRecordsResponse" message="tns:SearchDailyRecordsResponse"/>
<fault message="tns:Exception" name="Exception" wsam:Action="http://services.myapp.com:8080/myapp-1.0-SNAPSHOT/MyService/SearchDailyRecords/Fault/Exception"/>
</operation>
</portType>
<binding name="MyServicePortBinding" type="tns:MyService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="SearchDailyRecords">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="Exception">
<soap:fault name="Exception" use="literal"/>
</fault>
</operation>
</binding>
<service name="MyServiceService">
<port name="MyServicePort" binding="tns:MyServicePortBinding">
<soap:address location="http://services.myapp.com:8080/myapp-1.0-SNAPSHOT/MyService"/>
</port>
</service>
</definitions>

Java Backend Service:

 @Override
    public @WebResult(name = "response")
    List<SearchDailyRecordsResponse> SearchDailyRecords(@WebParam(name = "employeeId") @XmlElement(required = true) String employeeId, @WebParam(name = "date") @XmlElement(required = true) String date) throws Exception {                   

        SearchDailyRecordsRequest request = new SearchDailyRecordsRequest();
        request.employeeId = employeeId;        
        request.date = date;

        try {

            IDailyRecordDAO dailyRecordDAO = (IDailyRecordDAO) DAOFactory.getInstance().getDAO("DailyRecord");

            List<DailyRecord> dailyRecords = registroGenericoDAO.get(request.employeeId, request.date);
            List<SearchDailyRecordsResponse> response = new ArrayList<SearchDailyRecordsResponse>();

            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");        

            for (DailyRecord dailyRecord : dailyRecords) {
                SearchDailyRecordsResponse searchDailyRecords = new SearchDailyRecordsResponse();
                searchDailyRecords.recordId = dailyRecord.getRecord().getId();                   
                /* some other info populating record */ 

                response.add(searchDailyRecords);
            }
            return response;
        } catch (Exception e) {
                Logger.getLogger(MyService.class.getName()).log(Level.WARNING, null, e);
                throw new Exception("Error getting daily records");
        }
    }
}

PHP test script

<?php

require_once('../backend/libraries/nusoap/nusoap.php');

$url = "http://services.myapp.com:8080/myapp-1.0-SNAPSHOT/MyService?wsdl";

ini_set("soap.wsdl_cache_enabled", "0");

$client = new nusoap_client($url, 'wsdl');

try {
    echo 'try: ';

    /* date("d/m/Y") --> DATE FORMAT */

    $date = '06/05/2016';  

    $request = array(
        'employeeId' => 1234,
        'date' => $date
    );    


    $response = $client->call('SearchDailyRecords', array('request' => $request));    
    //die('<pre>'.print_r($client, true).'</pre>');


    if ($client->fault) {
    echo 'fault?: ';
        echo "<h2>Fault</h2><pre>";
        print_r($response);
        echo "</pre>";
    } else {
        $error = $client->getError();
        if ($error) {
            echo "<h2>Error</h2><pre>" . $error . "</pre>";
        } else {
            echo "<h2>Main</h2>";
            echo $response;
        }
    }

    // show soap request and response
    echo "<h2>Request</h2>";
    echo "<pre>" . htmlspecialchars(formatXmlString($client->request), ENT_QUOTES) . "</pre>";
    echo "<h2>Response</h2>";
    echo "<pre>" . htmlspecialchars(formatXmlString($client->response), ENT_QUOTES) . "</pre>";

    /******************************************/
} catch (SoapFault $exc) {
    die('<pre>' . print_r($exc, true) . '</pre>');
}

function formatXmlString($xml){
    $xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);
    $token      = strtok($xml, "\n");
    $result     = '';
    $pad        = 0; 
    $matches    = array();
    while ($token !== false) : 
        if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) : 
          $indent=0;
        elseif (preg_match('/^<\/\w/', $token, $matches)) :
          $pad--;
          $indent = 0;
        elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
          $indent=1;
        else :
          $indent = 0; 
        endif;
        $line    = str_pad($token, strlen($token)+$pad, ' ', STR_PAD_LEFT);
        $result .= $line . "\n";
        $token   = strtok("\n");
        $pad    += $indent;
    endwhile; 
    return $result;
}

?> 

After some research, I found what the problem was and how to fix it, so here I'm posting what I did in case somebody else comes accross a similar issue:

After requesting the Tomcat Server Log deployed to Development, i found this exception:

SEVERE: null
com.myappconfiguration.ConfigurationException: La variable DailyRecord no existe!
    at com.myapp.Configuration.getEnvironmentVariable(Configuration.java:17)
    at com.myapp.dao.DAOFactory.LoadDaoFromFile(DAOFactory.java:57)
    at com.myapp.dao.DAOFactory.getDAO(DAOFactory.java:42)
    at com.myapp.service.MyAppService.SeachDailyRecords(MyAppService.java:515)
    at sun.reflect.GeneratedMethodAccessor322.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:250)
    at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:149)
    at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:88)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1063)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:979)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:950)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:825)
    at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:380)
    at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:651)
    at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:264)
    at com.sun.xml.ws.transport.http.servlet.ServletAdapter.invokeAsync(ServletAdapter.java:218)
    at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:159)
    at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:194)
    at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:80)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: javax.naming.NameNotFoundException: Name [DailyRecord] is not bound in this Context. Unable to find [DailyRecord].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    at com.myappconfiguration.Configuration.getEnvironmentVariable(Configuration.java:14)

So i figured a new Class and DAO was not being loaded, which was resolved by adding the entry to my context.xml file (located in the META-INF folder)

<Environment name="DailyRecord" override="false" type="java.lang.String" value="com.myapp.dao.oracle.DailyRecordDAO"/>

After doing that i rebuilt the app, and deployed the newly generated .war file without problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM