Service-side Publishing Service 1.1 defining complex types: Userbean.java
Package Service; Public class UserBean { private String userId; Private String userName; Private String PassWord; // hide Getset Method }
1.2 Defining the Service interface:UserService. java
PackageService;ImportJavax.jws.WebParam;ImportJavax.jws.WebResult;ImportJavax.jws.WebService;/*** This is a Web service *@authorAdministrator **/@WebService Public InterfaceUserService {/*** Get user *@return */@WebResult (Name= "Getuserresult") PublicUserBean GetUser (); /*** Set User *@paramUser*/ Public voidSetUser (@WebParam (name = "UserBean")) UserBean user);}
1.3 Defining a Service implementation class
PackageService;ImportJavax.jws.WebParam;ImportJavax.jws.WebResult;ImportJavax.jws.WebService; @WebService (Endpointinterface= "service. UserService ") Public classUserserviceimplImplementsUserService {@Override PublicUserBean GetUser () {UserBean user=NewUserBean (); User.setpassword ("123"); User.setuserid ("001"); User.setusername ("User returned by the server"); returnuser; } @Override Public voidsetUser (UserBean user) {if(User! =NULL) {System.out.println ("Client-Set user =" +user); } }}
1.4 Publishing Services
Package Publish; Import Javax.xml.ws.Endpoint; Import service. Userserviceimpl; Public class testpublish { publicstaticvoid main (string[] args) { Endpoint.publish (new Userserviceimpl ()); System.out.println ("Publish successfully ....."); }
Second, the client receives, sends the message to the service side 2.1 uses the tool to generate the client code (the specific step refers to the previous chapter)
2.2 Sending and receiving messages
Packagetest;Importjava.rmi.RemoteException;Importservice. UserBean;Importservice. UserService;Importservice. Userserviceproxy; Public classTestmain { Public Static voidMain (string[] args)throwsRemoteException {/*** Get the user returned by the server*/UserService UserService=Newuserserviceproxy (). Getuserservice (); UserBean User=Userservice.getuser (); System.out.println (User.getuserid ()+ "\ T" +user.getusername ()); /*** Client Setting User*/UserBean UserBean=NewUserBean (); Userbean.setuserid ("2"); Userbean.setusername ("User set by Client"); Userbean.setpassword ("123520"); Newuserserviceproxy (). SetUser (Userbean); }}
Results:
- Note: For normal Java data types, JavaBean, List, the SOAP service can completely handle it (serialization and deserialization) without any problems, but for a Map object, no more complex types are allowed.
- In general, parameters only pass string types, because Web services can be called in different languages, while other languages for Java complex types may not, but all languages have string types. Instead, the passed string encapsulates the data into a json/xml-formatted string, and the client receives the Json/xml-formatted string for parsing.
(iv) Input parameters and output types are complex types of Web services