Restlet Jax-Rs Configuration

Source: Internet
Author: User

 

Configure the restlet JAX-RS extension-based Web service that deploys the Web service under that architecture. Restlet architecture provides two ways to deploy Web Services. Both methods are convenient and simple. You can select any deployment method as needed.

  • Deploy Web Service as a separate Java program
  • Deploy web service to servelet container

Both methods are convenient and simple. You can select any deployment method as needed.

Deploying a web service as a separate Java application is very simple. You only need to complete the following steps.

  • Import the required jar packages, org. restlet. jar, and org. restlet. Ext. jaxrs_1.0.jar.
  • Create a Java class for the HTTP server. Complete the following steps in the newly created Java class to introduce Org. restlet. create an HTTP server, define the listening port of the server, and add the Web service configuration class to the HTTP server.
  • Compile and run the HTTP server.

Deploying a restlet jax-rs extension-based Web service to a servelet container is very similar to deploying a basic servelet. The difference is that you need to add the required jar package during deployment. The following JAR files are required for this deployment method.

  • Org. restlet. Jar
  • Org. restlet. Ext. jaxrs_1.0.jar
  • Com. noelios. restlet. Jar
  • Com. noelios. restlet. Ext. servlet_2.5.jar

To successfully deploy the restlet jax-rs extension-based Web service as servelet, you need to complete the following operations.

  • Compile the Code contained in the restlet jax-rs extension-based Web service.
  • Store the required jar package in/WEB-INF/lib.
  • Create the configuration file web. xml of servelet.
  • Pack all related content into a war package and deploy it to the user-selected servelet container.

 

 


Package com. resource;

 

Import java. util. arraylist;

Import java. util. List;

 

Import javax. ws. Rs .*;

 

Import com. model. user;

Import com. model. usergroup;

Import com. model. usergroupmanager;

Import com. model. usermanager;

 

@ Path ("users ")

Public class jaxrsextensionresource {

 

@ Get

@ Path ("usergroup ")

Public String getusergroup (){

Return "group are used to classify different kind of users! ";

}

 

 

@ Get

@ Path ("user ")

Public String getuser (){

Return "users inlcudes the information of registered user! ";

}

 

@ Get

@ Path ("User/{ID }")

Public String finduser (@ pathparam ("ID") string ID ){

User temp = usermanager. Get (). getdetails (ID );

If (temp! = NULL)

Return temp. tostring ();

Else

Return "the user you queried (ID:" + ID + ") doesn' t existed! ";

}

 

@ Get

@ Path ("usergroup/{ID }")

Public String findusergroup (@ pathparam ("ID") string ID ){

Usergroup group = usergroupmanager. Get (). getdetails (ID );

If (group! = NULL)

Return group. tostring ();

Else

Return "the group you queried (ID:" + ID + ") doesn' t existed! ";

}

 

}

 

@ Path specifies the resource route, which can be a resource route or a resource method route.

@ Get specifies the request type (get, put, post, delete)

@ Pathparam corresponds to the parameter in @ path

 

 

First, Jax-Rs manages resources.


Package com. Application;

 

Import java. util. hashset;

Import java. util. Set;

 

Import javax. ws. Rs. Core. Application;

 

Import com. Resource. jaxrsextensionresource;

 

Public class exampleapplication extends application {

 

@ Override

Public set <class <?> Getclasses (){

Set <class <?> RRCs = new hashset <class <?> ();

RRCs. Add (jaxrsextensionresource. Class );

Return RRCs;

}

 

}

Note that the application of Jax-Rs is inherited here, rather than restlet.

Submit it to restlet for management.

To better support JAX-RS specifications, the restlet architecture sets the jaxrsapplication class to initialize the JAX-RS-based Web service runtime environment. The jaxrsapplication class is very convenient to use. You only need to add the originally restlet-based application class to your own jaxrsapplication subclass. If you need the authentication function, use the setguard (...) or setauthentication (...) method of jaxrsapplication. In this example, the authentication function is not set, so you only need to add the exampleapplication class to the jaxrsapplication subclass in this example.

 

 

Package com. Application;

 

Import org. restlet. context;

Import org. restlet. Ext. jaxrs. jaxrsapplication;

 

Public class jaxrsextensionapplication extends jaxrsapplication {

Public jaxrsextensionapplication (context ){

Super (context );

This. Add (New exampleapplication ());

}

 

Public static void main (){

System. Out. println ("hello ");

}

}

 

 

Deploy Web Service as a separate Java program

 

public class JaxRsExtensionServer {      public static void main(String[] args){          try{              Component component = new Component();              component.getServers().add(Protocol.HTTP, 8182);                      component.getDefaultHost().attach(new JaxRsExtensionApplication(null));              component.start();          }catch(Exception e){              e.printStackTrace();          }      }  }
Run the code and enter http: // localhost: portnum/users/usergroup in the browser to call the getusergroup method.
Deploy web service to servelet container
Deploying a restlet jax-rs extension-based Web service to a servelet container is very similar to deploying a basic servelet.
The difference is that you need to add the required jar package during deployment. Then, create the configuration file web. xml of servelet. The following is the configuration file for this example.
Finally, you need to package these packages into war packages and deploy them to the selected servelet container.
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param>        <param-name>org.restlet.application</param-name>        <param-value>           com.application.JaxRsExtensionApplication         </param-value>     </context-param>       <!-- Restlet adapter -->     <servlet>        <servlet-name>RestletServlet</servlet-name>        <servlet-class>           com.noelios.restlet.ext.servlet.ServerServlet         </servlet-class>     </servlet>       <!-- Catch all requests -->     <servlet-mapping>        <servlet-name>RestletServlet</servlet-name>        <url-pattern>/*</url-pattern>     </servlet-mapping>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
Publishing a project is a container, running, and entering http: // localhost: portnum/project name/users/usergroup. I cannot find any reason for publishing a project in chrome. Model class code package COM. model; public class user {private string username = "User sample"; private string id = ""; public user (string ID) {This. id = ID;} public user () {This. username = "name sample"; this. id = "id isample";} public user (string name, string ID) {This. username = Name; this. id = ID;} Public String getuser () {If (ID. equalsignorecase ("null") Return "the user you queried (ID:" + this. username + ") doesn' t ex Isted! "; Return" user-name: "+ this. username + "ID:" + this. ID + "/N";} public void setname (string username) {This. username = username;} Public String getname () {return this. username;} public void setid (string ID) {This. id = ID;} Public String GETID () {return ID;} Public String tostring () {If (ID. equalsignorecase ("null") Return "the user you queried (ID:" + this. username + ") doesn' t existed! "; Return" user-name: "+ this. username + "ID:" + this. ID + "/N" ;}} package COM. model; import Java. util. arraylist; import Java. util. list; public class usergroup {private string groupname = "User Group sample"; private string groupid = "User Group ID sample "; private list <string> userlist = new arraylist <string> (); Public usergroup (string name) {groupname = Name;} public usergroup () {} public usergroup (string Name, string ID) {This. groupname = Name; this. groupid = ID;} Public String getname () {return groupname;} public void setname (string name) {This. groupname = Name;} Public String GETID () {return this. groupid;} public void setid (string ID) {This. groupid = ID;} public void adduser (string ID) {This. userlist. add (ID);} public void deleteuser (string ID) {int location =-1; for (INT I = 0; (I <this. userlist. size () & L Ocation =-1; I ++) {string temp = This. userlist. get (I); If (temp. repeated signorecase (ID) Location = I;} This. userlist. remove (location);} Public String getgroup () {If (this. groupid. equalsignorecase ("null") Return "group (ID:" + this. groupname + ") does not existed! /N "; Return" group-ID: "+ this. groupid + "name:" + this. groupname + "/N";} Public String tostring () {If (this. groupid. equalsignorecase ("null") Return "group (ID:" + this. groupname + ") does not existed! /N "; Return" group-ID: "+ this. groupid + "name:" + this. groupname + "/N" ;}} package COM. model; import Java. util. arraylist; import Java. util. list; public class usergroupmanager {Private Static usergroupmanager manager = new usergroupmanager (); Private list <usergroup> grouplist = new arraylist <usergroup> (); public static usergroupmanager get () {return manager;} private usergroupmanager () {final usergroup group1 = new usergroup (); group1.setid ("1"); group1.setname ("sample group"); grouplist. add (group1);} public usergroup getdetails (string ID) {for (INT I = 0; I <grouplist. size (); I ++) {usergroup temp = This. grouplist. get (I); If (temp. GETID (). equalsignorecase (ID) return temp;} return New usergroup (ID, "null") ;}} package COM. model; import Java. util. arraylist; import Java. util. list; public class usermanager {Private Static usermanager manager = new usermanager (); public static usermanager get () {return manager ;} private list <user> userlist = new arraylist <user> (); Private usermanager () {final user user1 = new user (); user1.setid (integer. tostring (1); user1.setname ("Zhou Peng"); this. userlist. add (user1); final user user2 = new user (); user2.setid (integer. tostring (2); user2.setname ("Lu Hong Yong"); this. userlist. add (user2);} public user getdetails (string ID) {for (INT I = 0; I <this. userlist. size (); I ++) {user temp = This. userlist. get (I); If (temp. GETID (). equalsignorecase (ID) return temp;} return (new user (ID, "null "));}}

 

Address: http://www.ibm.com/developerworks/cn/java/j-lo-jsr311/index.html

 

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.