我在REST中有一个Web服务,使用Java设计并部署在Tomcat上。这是Web服务结构:
@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";
}
}
其中ConectorCliente
类是MySQL连接器和查询类。
所以,我用@GET
实际做 POST 工作测试了这一点,任何用户从ma Java FX应用程序输入的数据和信息都是直接的到webservice的数据库。
但是,我进行了更改,因此 CREATE 操作是通过响应实际 POST HTTP请求的Web服务执行的。
然而,当我运行客户端并添加一些信息时,参数就可以了,但是下次我输入不同的参数时它会输入相同的信息。我跑了好几次,我无法理解它。
这是客户代码:
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();
}
}
这是我在Java FX应用程序中执行操作时的代码:
String nombre = nombreTextField.getText();
String edad = edadTextField.getText();
String insertToDatabase = consumidor.insertarPersona(String.class, nombre, edad);
因此,当参数取自TextFields时,为什么第二,第三,第四等等POSTS 发布相同是很奇怪的。
答案 0 :(得分:1)
如果不使webTarget成为实例变量,而是使用每个使用它的方法创建一个新变量,那该怎么办呢。
public <T> T insertarPersona(Class<T> responseType, String nombre, String edad) throws ClientErrorException {
WebTarget = webTarget = client.target(BASE_URI).path("Personas");
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 {
WebTarget = webTarget = client.target(BASE_URI).path("Personas");
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);
}