The corresponding code in this document has been uploaded, which corresponds to the service in tutorial (1. For debugging convenience, both the service and client are placed in the same project, but in different packages. The two basic clients corresponding to this Article are located at com. neareast. test. cxf. client. in the consumer package, the ServiceTest class of the server is in com. neareast. test. cxf.
The corresponding code in this document has been uploaded, which corresponds to the service in tutorial (1. For debugging convenience, both the service and client are placed in the same project, but in different packages. The two basic clients corresponding to this Article are located at com. neareast. test. cxf. client. in the consumer package, the ServiceTest class of the server is in com. neareast. test. cxf.
The corresponding code in this document has been uploaded, which corresponds to the service in tutorial (1. For debugging convenience, both the service and client are placed in the same project, but in different packages. The two basic clients corresponding to this Article are located at com. neareast. test. cxf. client. in the consumer package, the ServiceTest class of the server is in com. neareast. test. cxf. server. service package. The Code address is as follows:
Http://download.csdn.net/detail/neareast/4411870
The most common situation is that we have a service's WSDL. Whether this service is maintained by ourselves or not, we can generate a client from this WSDL file, including a strong interface, and interact with the service through interfaces.
Using the WSDL2Java tool, you can generate JAX-WS clients through WSDL. WSDL2java has the following three methods:
- The command line
- The Maven Plugin
- Use WSDL2Java API
For more information, see examples in the Developing a JAX-WS consumer article or hairstyle package.
Eclipse and Maven plug-ins should eventually be implemented by calling the WSDL2Java command. Here we use the released Service and use it directly on the console.Wsdl2java http: // localhost: 9000/helloWorld? WsdlCommand, you can be in the command directory (for example, the author in the bin directory of the apache-cxf-2.6.1 package to call the wsdl2java command, the generated class file is in this directory ), generate the package path corresponding to targetNamespace In the WSDL and put the corresponding classes in this directory. The client can directly call these classes, the generated code is in ANSI format ......
However, the generated HelloWorld class reports an error, such as the super (WSDL_LOCATION, SERVICE, features) of the HelloWorld method. This line reports an error because of javax. xml. ws. the Service (URL, QName, WebServiceFeature []) constructor is missing. The new Service class is provided in the geronimo-jaxws_2.2_spec-1.1.jar package we introduced, but endorse is required to replace the Service class that comes with jre. To minimize the hassle of porting later, we can specify the-frontend parameter based on the prompts in the comment and regenerate all classes using the JAX-WS 2.1 compatibility mode, by the way, use the-p parameter to specify the namespace we need:
Wsdl2java-p com. neareast. test. cxf. client. WSDL2Java-frontend jaxws21 http: // localhost: 9000/helloWorld? Wsdl
Shows the generated classes. The HelloWorld class is used to start listening to services. The IHelloWorld class is used to call service interfaces.
Once a client is generated, the typical usage is as follows:
public class BasicClient {public static void main(String[] args){HelloWorld server = new HelloWorld();IHelloWorld hello = server.getHelloWorldImplPort();String result = hello.sayHi("East");System.out.println(result);User user = new User();user.setName("East");System.out.println(hello.sayHiToUser(user));System.out.println("All the users are: ");for(IdentifiedUser u:hello.getUsers().getEntry()){System.out.println( u.getUser().getName() );}}}
So far, a simple webService client has been completed.
JAX-WS agent
In addition to using wsdl2java to directly generate a client, we can also use Service. create to generate a Service instance. The following code shows the process:
URL url = null; try { url = new URL("http://localhost:9000/helloWorld?wsdl"); } catch (MalformedURLException e) { java.util.logging.Logger.getLogger(HelloWorld.class.getName()) .log(java.util.logging.Level.INFO, "Can not initialize the default wsdl from {0}", "http://localhost:9000/helloWorld?wsdl"); } WSDL_LOCATION = url;
Service service = Service.create(WSDL_LOCATION, SERVICE_NAME); IHelloWorld hw = service.getPort(IHelloWorld.class); System.out.println(hw.sayHi("World"));
NearEast