[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openejb-user
Subject:    spring integration with tomee cxf for REST api calls
From:       "Nicholas D. Thayer" <nicholas.thayer () broadcom ! com ! INVALID>
Date:       2022-12-09 3:30:28
Message-ID: ebe7455c-8958-bfe0-84ce-41d4e42222f6 () broadcom ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


I've added springframework support to a tomee webapp, but when REST 
endpoint is called the controller is created anew and @Autowired fields 
are not populated resulting in NPE being thrown.

Snippet of pom.xml:

    <dependency>
        <groupId>org.apache.tomee</groupId>
        <artifactId>javaee-api</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
   
   ...other dependencies


The web.xml mainly defines the spring ContextLoaderListener:

<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/appContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>


The appContext.xml defines some beans, but also specifies a 
context:component-scan specification as below to capture any 
annotation-driven components:

    <!-- specify the component scan -->
    <context:component-scan base-package="com.test" />


Within my application, I have a service and controller class.

@Component
public class TestService {
    public String getValue() {
        return "value01";
    }
}

@Component
@Path("/test")
public class TestController {

    @Autowired
    private TestService testService;

   public TestController() {
     System.out.println("TestController::constructor");
   }


   @PostConstruct
   public void init() {
     System.out.println("TestController::init. testService: " + testService);
   }

    @GET
    @Path("/value")
    @Produces("text/plain")
    public String getValue() {
     System.out.println("TestController::getValue. testService: " + testService);
        return testService.getValue();
    }
}


On webapp startup, the init() method is run and I'm seeing testService 
is populated with an object; therefore, the spring webappcontext is 
working as expected to populate the fields.

Now, when "GET /test/value" is called, the TestController object is 
created anew (constructor log statement shows this) but the testService 
is not being populated with @Autowire before the getValue() method is 
called.   The log output in getValue() is showing testService is null for 
this new TestController instance.

How to make CXF aware of the spring context so objects are retrieved as 
beans to honor @Autowire specifications during REST api method calls 
instead of calling the constructor and creating anew?




-- 
This electronic communication and the information and any files transmitted 
with it, or attached to it, are confidential and are intended solely for 
the use of the individual or entity to whom it is addressed and may contain 
information that is confidential, legally privileged, protected by privacy 
laws, or otherwise restricted from disclosure to anyone else. If you are 
not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, you are hereby notified that any use, 
copying, distributing, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender, delete it from your computer, and 
destroy any printed copy of it.

[Attachment #5 (text/html)]

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>I've added springframework support to a tomee webapp, but when
      REST endpoint is called the controller is created anew and
      @Autowired fields are not populated resulting in NPE being thrown.</p>
    <p>Snippet of pom.xml:</p>
    <pre>   &lt;dependency&gt;
       &lt;groupId&gt;org.apache.tomee&lt;/groupId&gt;
       &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
       &lt;scope&gt;provided&lt;/scope&gt;
   &lt;/dependency&gt;

   &lt;dependency&gt;
       &lt;groupId&gt;org.springframework&lt;/groupId&gt;
       &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
   &lt;/dependency&gt;

   &lt;dependency&gt;
       &lt;groupId&gt;org.springframework&lt;/groupId&gt;
       &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
   &lt;/dependency&gt;

   &lt;dependency&gt;
       &lt;groupId&gt;org.springframework&lt;/groupId&gt;
       &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
   &lt;/dependency&gt;
  
  ...other dependencies
</pre>
    <p><br>
    </p>
    <p>The web.xml mainly defines the spring ContextLoaderListener:<br>
    </p>
    <pre>&lt;web-app version="2.5"
   xmlns=<a class="moz-txt-link-rfc2396E" \
href="http://java.sun.com/xml/ns/javaee">"http://java.sun.com/xml/ns/javaee"</a>  \
xmlns:xsi=<a class="moz-txt-link-rfc2396E" \
href="http://www.w3.org/2001/XMLSchema-instance">"http://www.w3.org/2001/XMLSchema-instance"</a>
  xsi:schemaLocation=<a class="moz-txt-link-rfc2396E" \
href="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">"http://java.sun.com/xml/ns/javaee \
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</a>&gt;

   &lt;context-param&gt;
       &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
       &lt;param-value&gt;classpath:spring/appContext.xml&lt;/param-value&gt;
   &lt;/context-param&gt;

   &lt;listener&gt;
       &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;

&lt;/web-app&gt;
</pre>
    <p></p>
    <p><br>
    </p>
    <p>The appContext.xml defines some beans, but also specifies a
      context:component-scan specification as below to capture any
      annotation-driven components:<br>
    </p>
    <pre>   &lt;!-- specify the component scan --&gt;
   &lt;context:component-scan base-package="com.test" /&gt;</pre>
    <p></p>
    <p><br>
    </p>
    <p>Within my application, I have a service and controller class.<br>
    </p>
    <pre>@Component
public class TestService {
   public String getValue() {
       return "value01";
   }
}</pre>
    <pre>@Component
@Path("/test")
public class TestController {

   @Autowired
   private TestService testService;

  public TestController() {
    System.out.println("TestController::constructor");
  }


  @PostConstruct
  public void init() {
    System.out.println("TestController::init. testService: " + testService);
  }

   @GET
   @Path("/value")
   @Produces("text/plain")
   public String getValue() {
    System.out.println("TestController::getValue. testService: " + testService);
       return testService.getValue();
   }
}</pre>
    <p>
    </p>
    <p><br>
    </p>
    <p>On webapp startup, the init() method is run and I'm seeing
      testService is populated with an object; therefore, the spring
      webappcontext is working as expected to populate the fields.<br>
    </p>
    <p>Now, when "GET /test/value" is called, the TestController object
      is created anew (constructor log statement shows this) but the
      testService is not being populated with @Autowire before the
      getValue() method is called.   The log output in getValue() is
      showing testService is null for this new TestController instance.<br>
    </p>
    <p>How to make CXF aware of the spring context so objects are
      retrieved as beans to honor @Autowire specifications during REST
      api method calls instead of calling the constructor and creating
      anew?<br>
    </p>
    <p><br>
    </p>
    <p><br>
    </p>
    <p><br>
    </p>
  </body>
</html>

<br>
<span style="background-color:rgb(255,255,255)"><font size="2">This electronic \
communication and the information and any files transmitted with it, or attached to \
it, are confidential and are intended solely for the use of the individual or entity \
to whom it is addressed and may contain information that is confidential, legally \
privileged, protected by privacy laws, or otherwise restricted from disclosure to \
anyone else. If you are not the intended recipient or the person responsible for \
delivering the e-mail to the intended recipient, you are hereby notified that any \
use, copying, distributing, dissemination, forwarding, printing, or copying of this \
e-mail is strictly prohibited. If you received this e-mail in error, please return \
the e-mail to the sender, delete it from your computer, and destroy any printed copy \
of it.</font></span>


["smime.p7s" (application/pkcs7-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic