1

I have done @Autowired annotation and also @Component annotation is calling and called classes. Still, I am getting Null pointer exception in "ServiceResource.java" -> serviceValidator value as null.

Please help me know where I am going wrong?

ServiceResouce.java

package com.resolvebug.app.controller;
@Component
@Path("v1")
@Produces(APPLICATION_JSON)
public class ServiceResource {
    @Autowired
    private ServiceValidator serviceValidator;

    @POST
    @Path("/post/json")
    public ServiceManager employeeLogin(ServiceManager serviceManager) {
        return serviceValidator.hello(serviceManager);
    }
}

ServiceValidator.java

package com.resolvebug.app.validator;
@Component
public class ServiceValidator {
    @Autowired
    private ServiceManager sm;

    public ServiceManager hello(ServiceManager serviceManager) {
        sm.setAge(serviceManager.getAge());
        sm.setName(serviceManager.getName());
        return sm;
    }
}

ServiceManager.java

package com.resolvebug.app.handler;
@Component
public class ServiceManager {
    public String name;
    public int age;
    public ServiceManager() {
    }
    public ServiceManager(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>REST Web Application using Jersey Web Application</display-name>

    <servlet>
        <servlet-name>rest-server</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.resolvebug.app.controller</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>rest-server</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

application context

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <context:spring-configured />
    <context:annotation-config />
    <context:component-scan
        base-package="com.resolvebug.app.controller" />
    <mvc:annotation-driven />

</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.resolvebug.app</groupId>
    <artifactId>bk-services</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>bk-services</name>

    <properties>
        <jdk_version>1.8</jdk_version>
        <jersey_version>2.22.1</jersey_version>
        <servlet_version>3.1.0</servlet_version>
        <junit_version>3.8.1</junit_version>
        <springbeans_version>3.0.5.RELEASE</springbeans_version>
        <springcontext_version>3.0.2.RELEASE</springcontext_version>
        <springweb_version>5.0.5.RELEASE</springweb_version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet_version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit_version}</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springbeans_version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springcontext_version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springweb_version}</version>
        </dependency>


        <!-- Jersey -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey_version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey_version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>bk-services</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk_version}</source>
                    <target>${jdk_version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  • Possible duplicate of [Autowired not working, dependency is null](https://stackoverflow.com/questions/49905700/autowired-not-working-dependency-is-null) – dunni Apr 26 '18 at 07:58

1 Answers1

0

How about your project configuration? The integration between Spring and Jersey does not work out-of-the-box, because Jersey resource endpoints lifecycle is not aware of the Spring beans instances creation and configuration.

By default, Jersey creates a new instance of the endpoint resource at each request, without any integration with the Spring bean lifecyle and scoped instances management, so the @Autowired annotation will not be processed by Spring in any way.

You have to setup the Spring integration and support in Jersey and to do so you can add the Jersey Spring extensions dependencies to your project configuration.

If you use Maven, for Jersey up to version 2.25 you have to add the following dependency:

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.25</version>
</dependency>

For Jersey version 2.26 or above, the jersey-spring4 artifact is available.

This way, the Jersey endpoints will be managed as Spring beans, enabling the dependency injections through the @Autowired annotation.

Also ensure the Spring configuration (including the Application Context initialization and bean registration phases) is properly setted up.

I think the best way to face this configuration concerns is by using Spring Boot, and in the Holon Platform, the Open Source project I manage, we provide many auto-configuration facilities to use the Spring framework with Jersey (for example, the Spring JAX-RS beans automatic registration as Jersey resources, getting rid of the configuration parameters such as jersey.config.server.provider.packages).

You can check this example on GitHub to see how to setup a Jersey server with Spring integration simply using a Spring Boot starter.

Riccardo Righi
  • 226
  • 1
  • 6
  • I have added pom.xml and application context. Can you please have a look and let me know exactly where I have to make changes? I am new to this, so please help. – Aishwarya Gupta Apr 26 '18 at 11:32
  • Hi @AishwaryaGupta, in your pom you have to add the following dependency: ` org.glassfish.jersey.ext jersey-spring3 ${jersey_version}` – Riccardo Righi Apr 27 '18 at 10:26
  • Furthrmore, you have to ensure that all your Spring components are detected using the `component-scan` declaration, for example specifyng `base-package="com.resolvebug.app"` – Riccardo Righi Apr 27 '18 at 10:29
  • Also pay attention to mix and match different Spring artifact versions, or you can face unexpected bahaviors (in your pom I see different artifact versions for spring-context, spring-beans and spring-web) – Riccardo Righi Apr 27 '18 at 10:33