0

Not sure what I have done wrong. I have my data model

Waypoint.java:

@XmlRootElement
public class Waypoint {
    @XmlElement
    private String test;

     
    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

}

then in my waypoint service i have my post function: WaypointService.java

    @POST
    @Path("/waypoint")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Waypoint addState(Waypoint waypoint) throws Exception {     
        try { 
            return waypoint;       
        } catch (Exception e) { 
            logger.log(Level.SEVERE, null, e);
        }
    }

When i try to make the post call from postman I receive a 415 error: [Postman Post request] https://i.stack.imgur.com/1IM6u.png

Content-Type: application/json 
Accept: application/json

Also here are the main dependencies i have installed:

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>3.04</version>
        </dependency>
        
        <dependency>
            <groupId>org.glashfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>3.04</version>
        </dependency>
     
        
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>3.04</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.19</version>
        </dependency>

Any help with this issue would be nice, I've been stuck on this issue for some time and look up a lot of videos/forum help and still havent been able to find out the problem. Just to clarify! I have a post request sending in json data in the body, and I just want it to recieve the data and output it back (just for testing purposes).

PLSHELP
  • 21
  • 3
  • Your dependencies are complete wrong. All you need are jersey-servlet-container and jersey-media-json-jackson. Get rid of everything else. Also see the following link for configuring Jersey 2/3 https://stackoverflow.com/q/45625925/2587435 – Paul Samsotha Mar 19 '22 at 22:38

1 Answers1

0

The Waypoint.java produces an xml and @Path("/waypoint") seems to consume a JSON as well as produce a JSON. I think thats why you are getting unsupported media type error

Aditya Akella
  • 479
  • 1
  • 7
  • would i change the Produce to @Produces(MediaType.APPLICATION_XML)? – PLSHELP Mar 19 '22 at 18:09
  • Yes, can you try the same. Change both produce and consume, as I see you taking Waypoint as input param as well – Aditya Akella Mar 19 '22 at 18:18
  • Oh ok I think i am getting confused, how can i make my endpoint be able to take a json body response? Because that is what I am trying to do. I thought Waypoint waypoint would autobind the json body response to a java object. I'm not sure if you saw the imgur i tried to link, but it shows me trying to send a post request with the body being a json object, here is the link https://i.stack.imgur.com/1IM6u.png , let me edit my post to clarify exactly what i want my code to do – PLSHELP Mar 19 '22 at 18:26
  • If you want to accept a JSON, then the model should not have parameter with class Waypoint as it expects an XML and converts to POJO. You can try sending an xml itself from postman instead of JSON by selecting the arrow next to JSON in the image. This will confirm what I am saying. If you want a JSON as input you should have a java class without @XmlRootElement – Aditya Akella Mar 19 '22 at 18:33
  • Thank you for the insight! I've figured it out! I'm new to web application development in java and jersey so i was confused for a bit, but your explanation really helped out, thanks! – PLSHELP Mar 19 '22 at 18:47