0

I am trying to write a short program in jersey, using RESTful services and Tomcat 7. I am facing with a problem that I cannot deal with, even searching over the internet. My project is in maven. I would really appreciate if anyone could help me. Below there are some pieces of my program that i think where exactly can be my problem.

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.pluralsight</groupId>
    <artifactId>exercise-services</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>exercise-services</name>

    <build>
        <finalName>exercise-services</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- uncomment this to get JSON support -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>

    </dependencies>
    <properties>
        <jersey.version>2.2</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

ActivityResource.java

@Path("activities")
public class ActivityResource {
    private ActivityRepository activityRepository=new ActivityRepositoryStub();
    @POST
    @Path("activity")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

    public Activity createActivityParams(MultivaluedMap<String, String> formParams){
        System.out.println(formParams.getFirst("description"));
        System.out.println(formParams.getFirst("duration"));

        return null;
    }

While the full stack trace error is there:

javax.servlet.ServletException: java.lang.NullPointerException
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
    com.pluralsight.ActivityResource.createActivityParams(ActivityResource.java:30)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:140)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:158)
    org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:101)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:353)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:343)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:237)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:211)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:982)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:359)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Thank you in advance!

Abaya
  • 47
  • 1
  • 8

2 Answers2

0

use the annotation @FormParam.

@POST
@Path("activity")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void createActivityParams(@FormParam("description") String description, @FormParam("duration") String duration){
        System.out.println(description);
        System.out.println(duration);
}
Wilder Valera
  • 986
  • 9
  • 11
0

I believe the problem is with your data format in postman or form. If you see the error com.pluralsight.ActivityResource.createActivityParams(ActivityResource.java:30) it is indicating createActivityParams method is trying to access null value and my guess, it might be from your System.out.println(formParams.getFirst("description"));. please check your postman and resend, it should work.

Method: POST
URL: http://localhost:8080/exercise-services/webapi/activities/activity/
Body: x-www-form-urlencoded ( i.e MediaType.APPLICATION_FORM_URLENCODED in your @Consumes )

enter image description here

Mad-D
  • 4,479
  • 18
  • 52
  • 93