当我将POSTMAN与x-www-urlencoded一起使用时,Jersey总是产生状态415!为什么?

约翰·科

我正在尝试学习Jersey,这是基于Jax RS规范的Java REST框架。我正在从多元化网站上做一个教程,效果不是很好。但是无论如何,我已经到了要使用Google Chromes邮递员向我的服务提交url编码的表单参数的地步。

我用于资源方法的类称为ActivityResource。每个@GET带注释的方法都可以,但@POST方法不起作用。

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

不管怎样,如果我在任何一个路径参数前插入一个斜杠,重新排列注释标题或应用老式的“ application / x-www-form-urlencoded”参数,我总是会得到一个烂HTTP状态415-Usupported Media Type response 。有谁知道我想念的东西吗?我需要一个缺少的罐子吗?

@Path(“ activities”)公共类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_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”)字符串说明,@FormParam(“ duration”)字符串持续时间)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么将Random与硬编码的种子一起使用总是产生相同的结果?

与仆人一起处理常规表格帖子(application / x-www-form-urlencoded)

当我将 cbind 与来自 gsub 函数的对象一起使用时,为什么所有值都被引用?

为什么将std :: bind与lambda一起使用时会产生如此大的对象?

当我将 [::-1] 与变量一起使用时会发生什么?

为什么我的网址包含2x www。?

使用 Javascript 将 www 重定向到非 www url

自动将响应项放入下一个请求 (raw --> x-www-form) (POSTMAN)

将json转换为x-www-form-urlencoded

当我将curl -X Post与https一起使用时,所有内容是否都已加密?

当我使用express-generator时,为什么将.bin / www视为没有.js扩展名的javascript文件

当我将OpenGL与glut一起使用时,为什么在关闭窗口并重新打开窗口时却什么也没显示?

在将Pipework与Docker容器一起使用时,为什么我无法进行RTNETLINK操作?

为什么无法使用域名前缀“ www”解析我的网站?

当我将FragmentPagerAdapter和ViewPager一起使用时,为什么在活动中看到两个工具栏?

如何使用 webclient 发布 body x-www-form-urlencoded?

如何在Rails中使用x-www-form-urlencoded

为什么 Volley String 请求不能在 x-www-form-urlencoded 中使用 POST 方法发送 Body 参数?

当使用字符串作为请求正文时,为什么Axios使用Content-Type application / x-www-form-urlencoded发送我的POST请求?

Postman Chrome:表单数据,x-www-form-urlencoded和原始数据之间有什么区别

当我将 System.Threading.Timer 与 TimerTrigger 一起使用时会发生什么?

wget -v -t=5 --no-clobber -x http://www.website.com/ 当我在终端中运行这个命令时,这个网站会发生什么?

为什么https始终与www子域一起使用?

当我将 avg 与 count 和 sum 一起使用时,组函数的使用无效

Chrome为什么总是将我重定向到https://www.google.com

当我将SimpleDateFormat类与“ YYYY”一起使用时,应用程序崩溃

当我将 PickerView 与 textField 一起使用时隐藏键盘

当我将 :id 与 Vue 一起使用时,HTML 属性不可见

当我将 IterativeImputer 与 KNeighborsClassifier 一起使用时出现错误“未知标签类型:‘连续’”