2017-04-21 2 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

관련 문제