Asiertxo Asiertxo - 6 months ago 34
Java Question

Trouble with RESTful service returning String[] objects

I have developed a simple RESTful webservice that returns a String[] with info that it gets from a database. The thing is that I use netbeans and tend to use classes as clients to this webservice, as netbeans generates them automatically, and in this case I don't know how to handle what the webservice method is requesting. It has to do with Class generic classes, and I don't know what to do.
Here is the method from the webservice:

@GET
@Produces("application/json")
public String[] tramo(@QueryParam("tramo") String tramo) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {

Class.forName("oracle.jdbc.OracleDriver").newInstance();
Connection conexion = DriverManager.getConnection("jdbc:oracle:thin:@172.17.56.133:1521:TRACK", "oc","oc");
Statement stmt=conexion.createStatement();
ResultSet rs1=stmt.executeQuery("select IDT.ROWID, IDT.ID_ID, IDT.ID_PST_ID, IDT.ID_UM_ID,IDT.ID_POS, IDT.ID_PULSOS, IDT.ID_LANZAR, TEL_FISKERNEL.TEL_COLOR_EXT, TEL_FISKERNEL.TEL_SEC_MONT,TEL_FISKERNEL.TEL_PRS_FAM,TEL_FISKERNEL.TEL_SORTENES_FAM from IDT,TEL_FISKERNEL where IDT.ID_UM_ID = TEL_FISKERNEL.TEL_PIN and ID_PST_ID="+tramo+" order by ID_POS" );
int i=0;
while(rs1.next()){
pin[i]=rs1.getString("ID_UM_ID");
i++;
}
rs1.close();
stmt.close();
conexion.close();


return pin;


}


As you can see it returns a simple String[] object, and that is what I would like to get when calling this method, but when netbeans creates the client, its method is written like this:

public <T> T tramo(Class<T> responseType, String tramo) throws ClientErrorException {
WebTarget resource = webTarget;
if (tramo != null) {
resource = resource.queryParam("tramo", tramo);
}
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}


What should I give to this method to get a String[]? I have tried and read many tutorials online but I just don't understand what this method is requesting.
Thanks in advance fellas!

Answer

T is a generic for the type you want to use so the invocation would be something like this

String[] resp = tramo(String[].class, tramo)