REST web service keeps first POST parametrs
Posted
by
Diego
on Stack Overflow
See other posts from Stack Overflow
or by Diego
Published on 2013-10-31T15:44:06Z
Indexed on
2013/10/31
15:53 UTC
Read the original article
Hit count: 306
I have a web service in REST, designed with Java and deployed on Tomcat. This is the web service structure:
@Path("Personas")
public class Personas {
@Context
private UriInfo context;
/**
* Creates a new instance of ServiceResource
*/
public Personas() {
}
@GET
@Produces("text/html")
public String consultarEdad (@QueryParam("nombre") String nombre) {
ConectorCliente c = new ConectorCliente("root", "cafe.sql", "test");
int edad = c.consultarEdad(nombre);
if (edad == Integer.MIN_VALUE) return "-1";
return String.valueOf(edad);
}
@POST
@Produces("text/html")
public String insertarPersona(@QueryParam("nombre") String msg, @QueryParam("edad") int edad) {
ConectorCliente c = new ConectorCliente("usr", "passwd", "dbname");
c.agregar(msg, edad);
return "listo";
}
}
Where ConectorCliente
class is MySQL connector and querying class.
So, I had tested this with the @GET
actually doing POST work, any user inputed data and information from ma Java FX app and it went direct to webservice's database.
However, I changed so the CREATE operation was performed through a webservice responding to an actual POST HTTP request.
However, when I run the client and add some info, parameters go OK, but in next time I input different parameters it'll input the same. I run this several times and I can't get the reason of it.
This is the clients code:
public class WebServicePersonasConsumer {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/GetSomeRest/serviciosweb/";
public WebServicePersonasConsumer() {
client = javax.ws.rs.client.ClientBuilder.newClient();
webTarget = client.target(BASE_URI).path("Personas");
}
public <T> T insertarPersona(Class<T> responseType, String nombre, String edad) throws ClientErrorException {
String[] queryParamNames = new String[]{"nombre", "edad"};
String[] queryParamValues = new String[]{nombre, edad};
;
javax.ws.rs.core.Form form = getQueryOrFormParams(queryParamNames, queryParamValues);
javax.ws.rs.core.MultivaluedMap<String, String> map = form.asMap();
for (java.util.Map.Entry<String, java.util.List<String>> entry : map.entrySet()) {
java.util.List<String> list = entry.getValue();
String[] values = list.toArray(new String[list.size()]);
webTarget = webTarget.queryParam(entry.getKey(), (Object[]) values);
}
return webTarget.request().post(null, responseType);
}
public <T> T consultarEdad(Class<T> responseType, String nombre) throws ClientErrorException {
String[] queryParamNames = new String[]{"nombre"};
String[] queryParamValues = new String[]{nombre};
;
javax.ws.rs.core.Form form = getQueryOrFormParams(queryParamNames, queryParamValues);
javax.ws.rs.core.MultivaluedMap<String, String> map = form.asMap();
for (java.util.Map.Entry<String, java.util.List<String>> entry : map.entrySet()) {
java.util.List<String> list = entry.getValue();
String[] values = list.toArray(new String[list.size()]);
webTarget = webTarget.queryParam(entry.getKey(), (Object[]) values);
}
return webTarget.request(javax.ws.rs.core.MediaType.TEXT_HTML).get(responseType);
}
private Form getQueryOrFormParams(String[] paramNames, String[] paramValues) {
Form form = new javax.ws.rs.core.Form();
for (int i = 0; i < paramNames.length; i++) {
if (paramValues[i] != null) {
form = form.param(paramNames[i], paramValues[i]);
}
}
return form;
}
public void close() {
client.close();
}
}
And this this the code when I perform the operations in a Java FX app:
String nombre = nombreTextField.getText();
String edad = edadTextField.getText();
String insertToDatabase = consumidor.insertarPersona(String.class, nombre, edad);
So, as parameters are taken from TextFields, is quite odd why second, third, fourth and so on POSTS post the SAME.
© Stack Overflow or respective owner