【问题标题】:Post method is not working in JerseyPost 方法在泽西岛不起作用
【发布时间】:2017-09-18 06:27:21
【问题描述】:

我正在尝试使用 RESTful 服务和 Tomcat 7 使用 RESTful 服务和 Tomcat 7 编写一个短程序。我遇到了一个我无法处理的问题,即使在互联网上搜索也是如此。我的项目在 Maven 中。如果有人可以帮助我,我将不胜感激。下面有一些我的程序,我认为我的问题到底在哪里。

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)

提前谢谢你!

【问题讨论】:

  • 第 30 行是什么?
  • System.out.println(activity.getDescription());
  • 这是第 30 行。
  • 看看为什么activity 为空。
  • 其实我不是很明白,因为当我使用Get方法时,一切正常

标签: post jersey tomcat7 restful-architecture


【解决方案1】:

使用注解@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);
}

【讨论】:

    【解决方案2】:

    我认为问题在于邮递员或表单中的数据格式。如果您看到错误 com.pluralsight.ActivityResource.createActivityParams(ActivityResource.java:30),则表明 createActivityParams 方法正在尝试访问空值,我猜,它可能来自您的 System.out.println(formParams.getFirst("description"));。请检查您的邮递员并重新发送,它应该可以工作。

    方法:POST
    网址:http://localhost:8080/exercise-services/webapi/activities/activity/
    正文:x-www-form-urlencoded(即 @Consumes 中的 MediaType.APPLICATION_FORM_URLENCODED )

    【讨论】:

      猜你喜欢
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 2014-10-06
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      相关资源
      最近更新 更多