当我使用POSTMAN和x-www-urlencoded时,泽西总是会产生状态415!为什么?

问题描述:

我正试图学习基于Jax RS规范的Jersey,Java的REST框架。我正在从复数网站做一个教程,这不是很好。但无论如何,我已经开始使用Google Chromes邮递员将URL编码的表单参数提交给我的服务。当我使用POSTMAN和x-www-urlencoded时,泽西总是会产生状态415!为什么?

我用于资源方法的类称为ActivityResource。每个@GET注释的方法都有效,但不是@POST方法。

我提交的路径为localhost:8080 //的WebAPI /活动/活动

不管是什么,如果我在任何路径参数的前面插入斜线,重新注释标题或申请老式“application/x-www-form-urlencoded”参数我总是得到一个烂HTTP状态415 - Usupported媒体类型响应。有谁知道我错过了什么。我需要一个缺少的jar吗?

@Path( “活动”) 公共类ActivityResource {

private ActivityRepository activityRepository = new ActivityRepositoryStub(); 


@POST 
@Path("activity") 
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) 
public Activity createActivityParams(MultivaluedHashMap<String, String> formParams) { 

    System.out.println(formParams.getFirst("description")); 
    System.out.println(formParams.getFirst("duration")); 

    Activity activity = new Activity(); 
    activity.setDescription(formParams.getFirst("description")); 
    activity.setDuration(Integer.parseInt(formParams.getFirst("duration"))); 

    String id = String.valueOf(activityRepository.findAllActivities().size()); 
    activity.setId(id); 

    activityRepository.findAllActivities().add(activity); 

    return activity; 
} 

.....My Get methods down here which actually output functioning results 

}

这里是我的POM文件

http://maven.apache.org/maven-v4_0_0的.xsd“>

<modelVersion>4.0.0</modelVersion> 

<groupId>com.example</groupId> 
<artifactId>simple-service-webapp</artifactId> 
<packaging>war</packaging> 
<version>1.0-SNAPSHOT</version> 
<name>simple-service-webapp</name> 

<build> 
    <finalName>simple-service-webapp</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> 

    <dependency> 
     <groupId>org.glassfish.jersey.media</groupId> 
     <artifactId>jersey-media-moxy</artifactId> 
     <version>2.1</version> 
    </dependency> 

    <!-- 
    <dependency> 
     <groupId>org.glassfish.jersey.core</groupId> 
     <artifactId>jersey-client</artifactId> 
     <version>2.5.1</version> 
     <scope>provided</scope> 
    </dependency> 
    --> 


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

+0

它看起来像你发送错误的“内容类型”标头或没有。你的方法需要'APPLICATION_FORM_URLENCODED'数据。 – klimpond

好吧,我敢肯定,这个家伙给上pluralsight教程是会得到这个,而是由做一个小的搜索,所有的Java搜索无处不结果(MKyong)给了我一个很好的解决办法通过建议使用@ FormParam注释,而不是多值hashmap。哈利路亚

createActivityParams(@FormParam(“说明”)字符串递减,@FormParam(“时间”)字符串的持续时间)

对同一课程相同的问题进行搜索时看到这个。看后进一步发现问题...

问题是,当您需要使用多值映射的方法参数使用多值HashMap。

+0

接受的答案是使用多值散列图是错误的。你可能想补充一些关于你的答案为什么是正确的解释。 – Mark

+0

@标记此解决方案正在工作,并不真正知道使用MultivaluedHashMap时会导致问题的原因,但多值图是一种可行的方法。顺便说一句,接受的答案并没有告诉我们我们不能使用多值图。更重要的是,我也看过相同的Pluralsight教程,该教程显示您必须使用MultivaluedMap而不是MultivaluedHashMap,但是我犯了和OP一样的错误,并且使用了多值HashMap,这很糟糕。 :) – Benas