(iv) Input parameters and output types are complex types of Web services

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.