2013-10-31 49 views
0

我在REST中有一個Web服務,使用Java設計並部署在Tomcat上。這是Web服務結構:REST Web服務保持第一個POST參數

@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工作測試這一點,從馬的Java FX應用程序的任何用戶inputed數據和信息,並直接去到Web服務的數據庫。

然而,我改變所以CREATE通過一個web服務響應到實際POST HTTP請求進行操作。

但是,當我運行客戶端並添加一些信息時,參數會變好,但在下次輸入不同的參數時它會輸入相同的值。我跑了幾次,我無法得到它的原因。

這是客戶端代碼:

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); 

所以,作爲參數從TextField的拍攝,是相當奇怪爲什麼第二,第三,第四等POSTS 相同。

回答

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); 
}