0
votes

I'm having problems with the post requests in an API restful i'm testing and I don't see where is the error.

Here I've the API

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

import test.api.models.Test;
import test.api.MediaType;

@Path("/test")
public class testResource {

    @GET
    public String getSomething(){
        return "Ok GET";
    }

    @POST
    @Consumes(MediaType.TEST)
    public String postSomething(Test test){

        System.out.println("Got:  " + test);
        return "Ok POST";
    }

}

The problem is that when I do a get request to localhost:8080/test-api/test from postman application, I get "Ok GET". But If I do a post request I get 500 server error. I'm setting correctly the content-type header field. The error I get is:

"message": "HTTP 500 Internal Server Error",
"status": 500

and this is how I'm sending the data:

{
  "a": "one",
  "b": "two"
}

I think the problem can be in the Test class, because if I change the input parameter of the postSomething method to a String type I get the "Ok POST".

Here is the test class where I don't see anything strange...

public class Test {

    public String a;
    public String b;

    public Test ( String a, String b){
        this.a = a;
        this.b = b;
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

}

Anyone see something strange?

Thank you.

1

1 Answers

0
votes

The problem maybe the post data, maybe you have to url encode the post data. Try the following post data, it's url encoded.

%7B%22a%22%3A%20%22one%22%2C%22b%22%3A%20%22two%22%7D

or you can try http://meyerweb.com/eric/tools/dencoder/ to encode your own data.