Home > Software design >  RESTful API, Jersey, POST request 415 Unsupported Media Type
RESTful API, Jersey, POST request 415 Unsupported Media Type

Time:05-18

I am new to RESTful services and jersey. I am trying to send a POST request (using postman) for this function.

@Path("/bikes")
public class BikesResource {

    @POST
    @Consumes ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public void createBike(Bike bike) {
        BikeDao.instance.getModel().put(bike.getBikeID(),bike);
    }

This is my Bike Class:

@XmlRootElement
public class Bike {
    private String bikeID;
    private String ownerName;
    private String color;
    private String gender;

    public Bike(){

    }
    public Bike(String bikeID, String ownerName, String color, String gender){
        this.bikeID = bikeID;
        this.ownerName = ownerName;
        this.color = color;
        this.gender = gender;
    }

    public String getBikeDetails(){
        return getBikeID()   ", "   getOwnerName()   ", "   getColour()   ", "   getGender();
    }
    public String getBikeID(){
        return bikeID;
    }
    public String getOwnerName(){
        return ownerName;
    }
    public String getColour(){
        return color;
    }
    public String getGender(){
        return gender;
    }
    public void setBikeID(String bikeID){
        this.bikeID = bikeID;
    }
    public void setOwnerName(String ownerName){
        this.ownerName = ownerName;
    }
    public void setColour(String colour){
        this.color = colour;
    }
    public void setGender(String gender){
        this.gender = gender;
    }
}

The postman request looks like this: [1]: https://i.stack.imgur.com/N5US9.png

However I get 415 Unsupported Media type and I don't know why. I was thinking that maybe there is something wrong with my pom.xml, but I can't figure it out.

This is the pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>nl.utwente.di</groupId>
    <artifactId>bikeDealer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.30.1</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.30.1</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
        </dependency>

    </dependencies>
</project>

I would really appreciate some help. Thank you!

CodePudding user response:

Did you check if in Postman's 'Headers' section the ContentType is specified in 'application/json'? maybe the solution is there, it usually happens.

CodePudding user response:

It looks like you don't have JSON/XML Jersey modules to support the media you listed in @Consumes.

Check this doc: https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/media.html#json

and try:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.30.1</version>
</dependency>

(or better, use BOM to lock Jersey versions)

  • Related