문제

I'm trying to learn Jersey, Java's REST framework based on the Jax RS specification. I'm doing a tutorial from pluralsite which isn't that great. But anyway, I've gotten to a point where I'm using Google Chromes postman to submit url encoded form parameters to my service.

The class I'm using for my resource methods is called ActivityResource. Every @GET annotated method works but not the @POST method.

The path I'm submitting is localhost:8080//webapi/activities/activity

No matter what, if i insert a slash in front of either path parameter, rearrange the annotation headers or apply the old fashioned "application/x-www-form-urlencoded" argument I always get a rotten HTTP Status 415 - Usupported Media Type response. Does anyone have any idea what I'm missing. Is there a missing jar that I need?

@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(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

}

Here is my POM file

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>

도움이 되었습니까?

해결책

Okay, I'm sure the guy giving the tutorial on pluralsight is going to get to this but by doing a little search, the ubiquitous result of all java searches(MKyong) gave me a nice workaround by suggesting the use of @FormParam annotations rather that the Multivalued hashmap. hallelujah

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

다른 팁

Saw this when searching for the same issue on the same course. After looking further found the issue...

The issue is you are using the MultivaluedHashMap when you need to be using MultivaluedMap for the method parameter.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top