Bonjour,
J'ai un web service PHP utilisant NuSoap qui fonctionne sur une URL et donc voici la définition:
A coté de ça, j'ai un client Java chargé de requêter ce web service.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 <?php // load SOAP library require_once("libs/nusoap.php"); require_once("libs/functions.php"); // load library that holds implementations of functions we're making available to the web service // set namespace $ns="http://127.0.0.1:8989/real_mvp"; // create SOAP server object $server = new soap_server(); // setup WSDL file, a WSDL file can contain multiple services $server->configureWSDL('MvpSystem',$ns); $server->wsdl->schemaTargetNamespace=$ns; // register a web service method function search($query) { $results = array(); $tempArray = array('id' => '3', 'name' => 'MVP1', 'hp' => 'dsf', 'level' => '50' ); array_push($results, $tempArray); $tempArray = array('id' => '7', 'name' => 'MVP2', 'hp' => 'adada', 'level' => '79' ); array_push($results, $tempArray); return $results; } $server->wsdl->addComplexType('Mvp', 'complexType', 'struct', 'all', '', array( 'id' => array('name' => 'id', 'type' => 'xsd:int'), 'name' => array('name' => 'name', 'type' => 'xsd:string'), 'hp' => array('name' => 'hp', 'type' => 'xsd:string'), 'level' => array('name' => 'level', 'type' => 'xsd:int') ) ); $server->wsdl->addComplexType('Mvps', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:Mvp[]') ), 'tns:Mvp' ); $server->register('search', array('query' => 'xsd:string'), array('return' => 'tns:Mvps'), $ns, '$ns#search', 'rpc', 'encoded', 'Returns the data for every person' ); // service the methods $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); ?>
Voici le main :
Après avoir interrogé le web service, je tente d'afficher les valeurs retournées.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public static void main (String[] args){ BasicConfigurator.resetConfiguration(); BasicConfigurator.configure(); String targetNameSpace = "http://127.0.0.1:8989/real_mvp"; String endpoint = targetNameSpace+"/server.php"; String myOpName = "search"; Service service = new Service(); try{ Call call = ( Call )service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); QName qn = new QName(targetNameSpace, "Mvp" ); call.registerTypeMapping( Mvp.class, qn, new BeanSerializerFactory(Mvp.class,qn), new BeanDeserializerFactory(Mvp.class,qn) ); call.addParameter("query",XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.SOAP_ARRAY); Mvp[] ret = (Mvp[]) call.invoke(myOpName, new Object[] { "test" } ); System.out.println("Result = "+ret.length); Mvp cur = null; for (int i = 0; i < ret.length; i++){ cur = ret[i]; System.out.println("Mvp ["+cur.getId()+"] = "+cur.getName()+" has "+cur.getHp()+" HP."); System.out.println(cur.getLevel()); } }catch(RemoteException e){ System.out.println("Remote error : "+e.getMessage()); }catch(ServiceException e){ System.out.println("Service error : "+e.getMessage()); }catch(MalformedURLException e){ System.out.println("MalformedUrl error : "+e.getMessage()); } }
J'ai bien deux bean de type 'Mvp' qui sont créé, mais les données qui y sont mises ne sont pas bonnes. Voici les traces :
Le problème est clair : chaque propriété de mon objet Java 'Mvp' qui a le même type est rempli de la première valeur de ce type fournie par le web service.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 Result = 2 Mvp [3] = MVP2 has MVP2 HP. 3 Mvp [7] = MVP2 has MVP2 HP. 7
La valeur de l'ID est rempli à la fois dans la propriété 'id' et 'level' (au lieu de recevoir '79' ou '50' dans 'level')
De même que la valeur de 'name' est rempli à la fois dans la propriété 'name' et 'hp'. (au lieu de recevoir 'adada' ou 'dsf' dans 'hp')
Quelqun aurait-il une idée du pourquoi ?
En vous remerciant par avance pour votre temps.
Cordialement.
Partager