I am trying to pass an int from a Java client to a WebService Rest. I have seen that @PathParam can be used to pass a parameter so I have put this in the WebService.
@GET
@Path("/tipusMoviment/{moviment}")
@Produces(MediaType.TEXT_PLAIN)
public int tipusMoviment(@PathParam("moviment") int moviment) {
int result = 99; //si falla el if
if (moviment == 1) {
result = 5;
}else{
result = 3;
}
return result;
}
Now on the client I have the following:
public class NewJerseyClient {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/ProyectoREST1/webresources";
public NewJerseyClient() {
client = javax.ws.rs.client.ClientBuilder.newClient();
webTarget = client.target(BASE_URI).path("generic");
}
public <T> T tipusMoviment(Class<T> responseType, String moviment) throws ClientErrorException {
return webTarget.path(java.text.MessageFormat.format("tipusMoviment/{0}", new Object[]{moviment})).request().post(null, responseType);
}
public void close() {
client.close();
}
}
and in the main
public class ProyectoCliente7 {
public static void main(String[] args) {
NewJerseyClient ws = new NewJerseyClient();
System.out.println(ws.tipusMoviment(2));
}
}
I don't know what I'm doing wrong or what the problem is, I'm new to REST and I would appreciate some help because as much as I search the internet I can't stop finding things that don't work or I don't understand.
I have been told that using the ws.tipusMoviment(2) has to work but nevertheless on the internet I find things like this:
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/your-app/rest/data/post");
String input = "{\"message\":\"Hello\"}";
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, input);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
Thank you very much for your time.
You can try changing the notation:
@Produces(MediaType.TEXT_PLAIN)
By :
@Produces(MediaType.APPLICATION_JSON)
With that it should work.
I have managed to pass the number, for this I have used:
Client
sweater
Web Service REST