In the previous section, we have successfully defined a rest service and provided specific implementation, but we still need to run it.
In the previous sectionEquipmentIt lists the required jar (run in Tomcat) and optional jar (run as an independent application ). This section describes how to run the program as an Independent Application and how to run the program in Tomcat.
Tomcat (or other Web containers)
To run in a tomcat container, you must first define an application class:
Java code
- Import java. util. hashset;
- Import java. util. Set;
- Import javax. ws. Rs. Core. Application;
- Public class customerapplication extends application {
- Private set <Object> singletons = new hashset <Object> ();
- Private set <class <?> Empty = new hashset <class <?> ();
- Public customerapplication (){
- Singletons. Add (New customerresourceservice ());
- }
- @ Override
- Public set <class <?> Getclasses (){
- Return empty;
- }
- @ Override
- Public set <Object> getsingletons (){
- Return singletons;
- }
- }
Simple Description:
- Getclasses (): returns the classes of all defined service classes. Each request generates a new service object.
- Getsingletons (): returns instances of all defined service classes. Each request will reuse existing objects.
Because our data needs to be reused, because getclasses () returns NULL; getsingletons () returns an object implementation created.
With this application class, you need a specific servlet class to process specific calls. The application class is specified as an init-Param parameter of this servlet class.
The implementers of different JAX-RS have different servlet implementations. Here I select the implementation of Apache cxf, which corresponds to the servlet class:
Org. Apache. cxf. jaxrs. servlet. cxfnonspringjaxrsservlet
Therefore, we need to declare a servlet in Web. XML as follows:
Java code
- <Servlet>
- <Servlet-Name> rest </servlet-Name>
- <Servlet-class> <strong> org. Apache. cxf. jaxrs. servlet. cxfnonspringjaxrsservlet </strong> </servlet-class>
- <Init-param>
- <Param-Name> <strong> javax. ws. Rs. Application </strong> </param-Name>
- <Param-value> <strong> com. restfully. Shop. Services. customerapplication </strong> </param-value>
- </Init-param>
- </Servlet>
- <Servlet-mapping>
- <Servlet-Name> rest </servlet-Name>
- <URL-pattern>/* </url-pattern>
- </Servlet-mapping>
The content of the final web. xml file is as follows:
Java code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: Web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- Id = "webapp_id" version = "2.5">
- <Display-Name> jaxrsservice </display-Name>
- <Welcome-file-List>
- <Welcome-File> index.html </welcome-File>
- <Welcome-File> index.htm </welcome-File>
- <Welcome-File> index. jsp </welcome-File>
- <Welcome-File> default.html </welcome-File>
- <Welcome-File> default.htm </welcome-File>
- <Welcome-File> default. jsp </welcome-File>
- </Welcome-file-List>
- <Display-Name> archetype created web application </display-Name>
- <Servlet>
- <Servlet-Name> rest </servlet-Name>
- <Servlet-class> org. Apache. cxf. jaxrs. servlet. cxfnonspringjaxrsservlet </servlet-class>
- <Init-param>
- <Param-Name> javax. ws. Rs. Application </param-Name>
- <Param-value> com. restfully. Shop. Services. customerapplication </param-value>
- </Init-param>
- </Servlet>
- <Servlet-mapping>
- <Servlet-Name> rest </servlet-Name>
- <URL-pattern>/* </url-pattern>
- </Servlet-mapping>
- </Web-app>
This rest service has been implemented and can be run now. Right-click the project and choose run as> run on server.
If no server is configured, a Web server is required. After the configuration is complete, the project runs automatically on this server. Finally, we can test it. We recommend using soapui for testing.
Assume that Tomcat is selected, and the context path of Tomcat is http: // localhost: 8080/. If the project name is jaxrsdemo, the root path of the rest service is:
Http: // localhost: 8080/jaxrsdemo/Mers MERS; it provides a wadl file in the following path:
Http: // localhost: 8080/jaxrsdemo/Mers MERs /? _ Wadl.
Directly import http: // localhost: 8080/jaxrsdemo/customers /? _ Wadl to soapui, the corresponding request method and structure will be automatically generated, just fill in the test content.
Independent applications
There are several differences with Web containers:
- You must provide your own context path, that is, the http: // localhost: 8080/jaxrsdemo section above.
- You need to control start and stop
If you lookOrg. Apache. cxf. jaxrs. servlet. cxfnonspringjaxrsservletThe source code will find that the key classes are:Org. Apache. cxf. jaxrs. jaxrsserverfactorybean.
We need to getOrg. Apache. cxf. jaxrs. jaxrsserverfactorybeanObject To obtainOrg. Apache. cxf. endpoint. ServerObject, thisOrg. Apache. cxf. endpoint. ServerThe object can be understood as a tomcat.
ReferenceOrg. Apache. cxf. jaxrs. servlet. cxfnonspringjaxrsservletIs as follows:
Java code
- Jaxrsserverfactorybean bean = resourceutils. createapplication (
- New customerapplication (), false );
- String address = "http: // localhost: 8008 /";
- Bean. setaddress (Address );
- Server = bean. Create ();
- Server. Start ();
- Try {
- Thread. Sleep (100000000 );
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- Server. Stop ();
Http: // localhost: 8008/is equivalent to http: // localhost: 8080/jaxrsdemo. Therefore, the path of the rest service is http: // localhost: 8008/Mers MERs, the corresponding service definition file is:
Http: // localhost: 8008/Mers MERs /? _ Wadl.
The remaining test process is the same as above.