rest - 当我使用带有 x-www-urlencoded 的 POSTMAN 时, Jersey 总是产生状态 415!为什么?

标签 rest jersey jax-ws postman

我正在尝试学习基于 Jax RS 规范的 Java REST 框架 Jersey。我正在从复数网站做一个不太好的教程。但无论如何,我已经到了使用 Google Chrome postman 将 url 编码的表单参数提交给我的服务的地步。

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

我提交的路径是 localhost:8080//webapi/activities/activity

无论如何,如果我在任一路径参数前插入斜杠,重新排列注释 header 或应用老式的“application/x-www-form-urlencoded”参数,我总是会得到一个烂 HTTP Status 415 - Usupported Media Type 响应.有谁知道我错过了什么。我需要一个缺少的 jar 吗?

@Path("事件")
公共(public)类 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>

最佳答案

好的,我确定提供有关复数视力教程的人会做到这一点,但是通过进行一些搜索,所有 java 搜索的普遍结果(MKyong)通过建议使用 @FormParam 注释而不是给我一个很好的解决方法多值哈希图。哈利路亚

createActivityParams(@FormParam("description") String desc, @FormParam("duration") String duration)

关于rest - 当我使用带有 x-www-urlencoded 的 POSTMAN 时, Jersey 总是产生状态 415!为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21740954/

相关文章:

路由的 REST API 最佳实践

rest - 使用 intel.xdk 进行 https 调用

java - 没有注释的 Jersey 方法参数

web-services - 如何更好地将 Jersey restful 启用类之间的对象传输到 JSP 页面

java - Weblogic 12c : Unable to add security token for identity, 但独立工作

rest - 在 REST 中,当资源被分页时,应该如何处理对 findAll 操作的 GET 请求?

python - Django 休息框架 {"detail":"Authentication credentials were not provided."}

java - hibernate 如何将一个实体扩展到所有实体

java - 在请求的方法中访问 SOAP 消息头

java - 从 JAX-WS 服务返回大文件