Jersey realizes Restful WebService and Ajax access _restful

Source: Internet
Author: User
Tags glassfish



JSP


<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()

;

String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %>





Package org.nercita.zmx;


Import Org.codehaus.jackson.jaxrs.JacksonJsonProvider;

Import Org.glassfish.jersey.server.ResourceConfig;


public class Restapplication extends ResourceConfig {public



restapplication () {


    //service class is in the package path  

    packages (" Org.nercita.zmx ");  

    Register JSON Converter  

    register (jacksonjsonprovider.class);

   

}









Xml



<?xml version= "1.0" encoding= "UTF-8"?> <!--this web.xml the ' not ' required when using Servlet 3.0 container, Implementation Details http://jersey.java.net/nonav/documentation/latest/jax-rs.html--> <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" > < servlet> <servlet-name>jersey Web application</servlet-name> <servlet-class>org.glass fish.jersey.servlet.servletcontainer</servlet-class> <!--<init-param> &LT;PARAM-NAME&G T;jersey.config.server.provider.packages</param-name> <param-value>org.nercita.zmx</param-value > </init-param>--> <init-param> <param-name>javax.ws.rs.application </param-name> &LT;PARAM-VALUE&GT;org.nercita.zmx.restapplication</param-value> </init-param> &LT;LOAD-ON-STARTUP&G t;1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey Web applic ation</servlet-name> <url-pattern>/webapi/*</url-pattern> </servlet-mapping> </web-

App>






Pom.xml



<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:s chemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>org.nercita.ltxx</groupId> <artifactid>restdemo</ar tifactid> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>

           restdemo</name> <build> <finalName>RestDemo</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifact Id>maven-compiler-plugin</artifactid> <version>2.5.1</version> <in Herited>true</inherited> <configuration> &LT;SOURCE&GT;1.7&LT;/SOURCE&G

                   T <target>1.7</target> </configuration> </plugin> </plugins> &

               lt;/build> <dependencyManagement> <dependencies> <dependency>

               <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId>

               <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagem ent> <dependencies> <dependency> <groupid>org.glassfish.jersey.cont Ainers</groupid> <artifactId>jersey-container-servlet-core</artifactId> <!-- Use the following artifactid if you don ' t need servlet 2.x compatibility--> <!--artifactid>jersey-c Ontainer-servlet</artIfactid--> </dependency> <!--uncomment this to get JSON support <depende Ncy> <groupId>org.glassfish.jersey.media</groupId> <artifactid>jersey-media-m Oxy</artifactid> </dependency>--> <!--Jackson JSON support--> &LT;DEPENDENCY&G

   T <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> < version>1.9.12</version> </dependency> <dependency> <groupId>org.codehaus.jackson< /groupid> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.12</version> & lt;/dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId> jackson-jaxrs</artifactid> <version>1.9.12</version> </dependency> </dependen Cies> &LT;properties> <jersey.version>2.22.2</jersey.version> <project.build.sourceencoding>u

Tf-8</project.build.sourceencoding> </properties> </project>




 



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.