2017-04-21 17 views
0

私はジャージーで、RESTfulなサービスとTomcat 7を使って短いプログラムを書こうとしています。私は対処できない問題に直面しています。私のプロジェクトは完成です。誰かが私を助けることができたら本当に感謝します。下に私のプログラムのいくつかの部分がありますが、私の問題がどこにあるのか正確に考えることができます。投稿方法がジャージーでは機能していません

の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; 
    } 

フルスタックトレースエラーがあるが:

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) 

は、事前にありがとうございます!

+0

何のライン30? –

+0

System.out.println(activity.getDescription()); – Abaya

+0

これは30行目です。 – Abaya

答えて

0

@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); 
} 
0

郵便配達員や書式のデータ形式に問題があると思います。 com.pluralsight.ActivityResource.createActivityParams(ActivityResource.java:30)というエラーが表示された場合は、createActivityParamsメソッドがnull値と私の推測にアクセスしようとしていることを示しています。これはSystem.out.println(formParams.getFirst("description"));からのものです。郵便配達員を確認して再送してください。

方法:POST
URL:http://localhost:8080/exercise-services/webapi/activities/activity/
本体:(あなたの@ConsumesにすなわちMediaType.APPLICATION_FORM_URLENCODED)x-www-form-urlencodedで

enter image description here

関連する問題