Jersey always produces 415 status when I use POSTMAN with x-www-urlencoded! What for?

I am trying to learn the basics of Java REST based on the Jax RS specification. I am making a textbook from pluralis, which is not so great. But in any case, I got to the point that I use the Google Chromes mail agent to send form parameters encoded in the URL to my service.

The class that I use for my resource methods is called ActivityResource. Each annotated @GET method works, but not the @POST method.

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

Regardless of the fact that if I insert a slash before any path parameter, rearrange the annotation headers, or use the old argument "application / x-www-form-urlencoded", I always get rotten HTTP status 415 - Usupported Media Type Response. Does anyone know what I am missing. Is there a missing jar that I need?

@Path ("activity") 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>

+4
source share
2 answers

, , , , , , , java (MKyong) , @ FormParam .

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

+3

. ...

, MultivaluedHashMap, MultivaluedMap .

+3

Source: https://habr.com/ru/post/1526631/


All Articles