JAX-RS entry 2: Run

Source: Internet
Author: User
Tags soapui
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
  1. Import java. util. hashset;
  2. Import java. util. Set;
  3. Import javax. ws. Rs. Core. Application;
  4. Public class customerapplication extends application {
  5. Private set <Object> singletons = new hashset <Object> ();
  6. Private set <class <?> Empty = new hashset <class <?> ();
  7. Public customerapplication (){
  8. Singletons. Add (New customerresourceservice ());
  9. }
  10. @ Override
  11. Public set <class <?> Getclasses (){
  12. Return empty;
  13. }
  14. @ Override
  15. Public set <Object> getsingletons (){
  16. Return singletons;
  17. }
  18. }

 

Simple Description:

  1. Getclasses (): returns the classes of all defined service classes. Each request generates a new service object.
  2. 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
  1. <Servlet>
  2. <Servlet-Name> rest </servlet-Name>
  3. <Servlet-class> <strong> org. Apache. cxf. jaxrs. servlet. cxfnonspringjaxrsservlet </strong> </servlet-class>
  4. <Init-param>
  5. <Param-Name> <strong> javax. ws. Rs. Application </strong> </param-Name>
  6. <Param-value> <strong> com. restfully. Shop. Services. customerapplication </strong> </param-value>
  7. </Init-param>
  8. </Servlet>
  9. <Servlet-mapping>
  10. <Servlet-Name> rest </servlet-Name>
  11. <URL-pattern>/* </url-pattern>
  12. </Servlet-mapping>

The content of the final web. xml file is as follows:

Java code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  3. Xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: Web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  4. Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. Id = "webapp_id" version = "2.5">
  6. <Display-Name> jaxrsservice </display-Name>
  7. <Welcome-file-List>
  8. <Welcome-File> index.html </welcome-File>
  9. <Welcome-File> index.htm </welcome-File>
  10. <Welcome-File> index. jsp </welcome-File>
  11. <Welcome-File> default.html </welcome-File>
  12. <Welcome-File> default.htm </welcome-File>
  13. <Welcome-File> default. jsp </welcome-File>
  14. </Welcome-file-List>
  15. <Display-Name> archetype created web application </display-Name>
  16. <Servlet>
  17. <Servlet-Name> rest </servlet-Name>
  18. <Servlet-class> org. Apache. cxf. jaxrs. servlet. cxfnonspringjaxrsservlet </servlet-class>
  19. <Init-param>
  20. <Param-Name> javax. ws. Rs. Application </param-Name>
  21. <Param-value> com. restfully. Shop. Services. customerapplication </param-value>
  22. </Init-param>
  23. </Servlet>
  24. <Servlet-mapping>
  25. <Servlet-Name> rest </servlet-Name>
  26. <URL-pattern>/* </url-pattern>
  27. </Servlet-mapping>
  28. </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:

  1. You must provide your own context path, that is, the http: // localhost: 8080/jaxrsdemo section above.
  2. 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
  1. Jaxrsserverfactorybean bean = resourceutils. createapplication (
  2. New customerapplication (), false );
  3. String address = "http: // localhost: 8008 /";
  4. Bean. setaddress (Address );
  5. Server = bean. Create ();
  6. Server. Start ();
  7. Try {
  8. Thread. Sleep (100000000 );
  9. } Catch (interruptedexception e ){
  10. E. printstacktrace ();
  11. }
  12. 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.

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.