4

Jax RS 仕様に基づく Java の REST フレームワークである Jersey を学習しようとしています。私は、それほど素晴らしいものではないpluralsiteからチュートリアルを行っています。とにかく、Google Chromes の郵便配達員を使用して、URL エンコードされたフォーム パラメータをサービスに送信するところまで来ました。

リソース メソッドに使用しているクラスは、ActivityResource と呼ばれます。すべての @GET アノテーション付きメソッドは機能しますが、@POST メソッドは機能しません。

提出するパスは localhost:8080//webapi/activities/activity です

いずれかのパス パラメーターの前にスラッシュを挿入するか、注釈ヘッダーを再配置するか、昔ながらの「application/x-www-form-urlencoded」引数を適用すると、常に腐った HTTP ステータス 415 - サポートされていないメディア タイプの応答が返されます。 . 誰かが私が見逃しているものを知っていますか? 私が必要とする不足している瓶はありますか?

@Path("アクティビティ") 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(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>

4

2 に答える 2

3

同じコースで同じ問題を検索しているときにこれを見ました。さらに調べたところ、問題が見つかりました...

問題は、メソッド パラメーターに MultivaluedMap を使用する必要があるときに、MultivaluedHashMap を使用していることです。

于 2014-03-20T17:25:05.530 に答える
3

さて、pluralsight のチュートリアルを提供している人はこれに到達すると確信していますが、少し検索を行うことで、すべての Java 検索 (MKyong) のユビキタスな結果は、代わりに @FormParam 注釈の使用を提案することで、私に素晴らしい回避策を与えてくれました。その多値ハッシュマップ。ハレルヤ

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

于 2014-02-17T22:35:31.163 に答える