0

I am writing a simple rest service using springframework and i am getting below error message while trying to access my rest service through chrome browser.

Error Processing Request

Context Path:/simple_app-1.0-SNAPSHOT

Servlet Path:/

Path Info:null

Query String:null

Stack Trace

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:979)

org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)

javax.servlet.http.HttpServlet.service(HttpServlet.java:687)

org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)

here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <name>SIMPLE_REST_V1</name>
    <groupId>com.rest.app</groupId>
    <artifactId>simple_app</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.2.6.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-portlet</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>true</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

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

    <servlet>
        <servlet-name>RestAPI</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>RestAPI</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

and context.xml

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

    <context:component-scan base-package="com.sumit.rest.app"/>
    <mvc:annotation-driven/>

</beans>

RESTController

@Controller
@RequestMapping(value = "/")
public class RESTController {
    private static AtomicLong counter = new AtomicLong();
    private final static String template = "Hello, %s";

    @RequestMapping(method = RequestMethod.GET, headers = "Accept=application/json")
    @ResponseBody
    public Map<String, String> get() {
        Map<String,String> map = new HashMap<>();
        map.put("value","this is the default response");
        return map;
    }


    @RequestMapping(value = "/greet", method = RequestMethod.GET, headers = "Accept=application/json", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Greeting getGreeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        Greeting greet =  new Greeting(counter.incrementAndGet(), String.format(template, name));
        System.out.println(greet);
        return greet;
    }
 }

and finally Greet.java

@JsonAutoDetect
public class Greeting {
    public final long id;
    public final String message;

    public Greeting(long id, String message) {
        this.id = id;
        this.message = message;
    }

    public long getId() {
        return id;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public String toString() {
        return "id = "+this.id + " and name = "+this.message;
    }
}

could you please point out where am i missing or making mistake ?

the response which i am expecting on the browser is the json representation of the Greet class's object.

the above code is sample code from https://spring.io/guides/gs/rest-service/ but the only difference is that what i am trying is the non-spring boot version of that sample.

below is the screenshot of the error enter image description here

any help would be appreciated !

Thanks in anticipation.

sumit
  • 3,210
  • 1
  • 19
  • 37

1 Answers1

0
  1. There are 2 kinds of application contexts set up in your case

(which is decribed clear enough here Purpose of ContextLoaderListener – Spring MVC and here).

In short If you've configured everything in one spring configuration file then you don't need the ContextLoaderListener, while you use

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

it will search for applicationContext.xml under WEB-INF, I guess you don't have this file so just remove this declaration.

enter image description here

enter image description here

  1. If you use wildFly 8.1, upgrade it to newer version.
Community
  • 1
  • 1
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
  • I tried it but it is still not working. I have attached the screenshot could you please have a look at it. – sumit Nov 13 '16 at 17:54
  • This *null pointer* exception comes from the API classes, as you see it works just fine on me. There are some quite similar reports from WildFly - 8.1 users and older JBoss , if this is your case then upgrade the wildfly to ver.8.2 – Fevly Pallar Nov 13 '16 at 18:45
  • How do you get to know if i am using WildFly - 8.1 ? – sumit Nov 13 '16 at 18:48
  • Yes ! I tried this in tomcat and yes it is working in there even with the contextLoaderListener. Any idea to run it with Wildfly 8.1 ? – sumit Nov 13 '16 at 18:54
  • Nah, just upgrade it, a lot of people had got through hard times using widlfly with (especially) Spring boot, and I don't want you to be one of those people. – Fevly Pallar Nov 13 '16 at 18:58