Tengo una aplicación WebService lado cliente en JAVA y lado servidor en PHP.
Estoy intentando enviar una solicitud con parametros json por CURL command como se ve a continuación:
curl -H 'Content-Type:application/json' -X PUT -d path=4920 -d message="I love you" http://localhost:4321/idjserver/index.php/setCancion/
El WebService esta construido en SlimFrameWork de la siguiente manera:
<?php
header('Access-Control-Allow-Origin: *');
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->put('/setCancion/', function () use ($app) {
$put=$app->request()->put();
var_dump($put);
});
$app->run();
De manera que la respuesta del comando CURL es:
Respuesta CURL:
array(2) {
["path"]=>
string(4) "4920"
["message"]=>
string(7) "I love you"
}
El comando CURL se comporta como es esperado, pero por otro lado tengo cliente JAVA con Jersey donde no puedo enviar los parametros json de forma correcta, estoy intentándolo con dos diferentes formas, este es mi código:
package idjplaymp4;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientResponse;
public class ClienteServicioWeb {
private static final String servicePath= "http://localhost:4321/idjserver/index.php";
private Client client = null;
private WebTarget target = null;
private final Gson gson = new Gson();
public ClienteServicioWeb() {
client = ClientBuilder.newClient();
target = client.target(servicePath);
}
/**** Get Request *****/
public Genero[] getMenu()
{
String menu = target.path("/getMenu").request(MediaType.APPLICATION_JSON).get(String.class);
Genero[] g = gson.fromJson(menu, Genero[].class);
return g;
}
/*** PRIMER FORMA *************/
public void addSong(String songPath){
JsonObject inputJsonObj = new JsonObject();
inputJsonObj.addProperty("path", songPath);
inputJsonObj.addProperty("message", "Hello World");
System.out.println(target.path("/setCancion/").request(MediaType.APPLICATION_JSON)
.put(Entity.entity(inputJsonObj,MediaType.APPLICATION_JSON), JsonObject.class));
}
/*** SEGUNDA FORMA *************/
public void addSong(String songPath){
String parametros="{\"path\":\""+songPath+"\"}";
String result = target.path("/setCancion/")
.request(MediaType.APPLICATION_JSON)
.put(Entity.entity(parametros, MediaType.APPLICATION_JSON), String.class);
System.out.println("Result: "+result);
}
}
la PRIMERA FORMA me retorna el siguiente error:
MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.google.gson.JsonObject, genericType=class com.google.gson.JsonObject.
La SEGUNDA FORMA me retorna un array vacio:
Result: array(0) {
}
Puede alguien echarme una mano!