`
louisling
  • 浏览: 143084 次
  • 性别: Icon_minigender_1
  • 来自: ZhuHai
社区版块
存档分类
最新评论

Build Simple Restful Web App with Maven and Jersey

阅读更多
1) Create a New Project Layout
cd/d C:\tmp\restful
mvn archetype:create -DgroupId=my.example.com -DartifactId=SimpleRESTApp -DarchetypeArtifactId=maven-archetype-webapp
It will create the following subtree:
SimpleRESTApp
   |-src
   |---main
   |-----resources
   |-----webapp
   |-------WEB-INF


2) Creating a REST Resource Class
package com.example.my;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;

// The Java class will be hosted at the URI path "/greeting"
@Path("greeting")
public class SimpleResource {

    // and implement the following GET method 
    @GET 
    @Produces("text/plain")
    public String getGreeting() {
        return "Hi there";
    }
}

3) Adding Necessary Dependencies Into Our POM,
C:\tmp\restful\SimpleRESTApp\pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.example.com</groupId>
  <artifactId>SimpleRESTApp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SimpleRESTApp Maven Webapp</name>
  <url>http://maven.apache.org</url>

<repositories>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven</name>
      <url>http://download.java.net/maven/2/</url>
      <layout>default</layout>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.1.0-ea</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.1.0-ea</version>
    </dependency>
    <dependency>
        <groupId>javax.ws</groupId>
        <artifactId>jsr311</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.objectweb</groupId>
        <artifactId>asm</artifactId>
        <version>3.1</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
    </plugins>
    
    <finalName>SimpleRESTApp</finalName>
</build>

</project>

4) Configuring Jersey Servlet Adapter
C:\tmp\restful\SimpleRESTApp\src\main\webapp\WEB-INF\web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
        <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
    </init-param>

    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.example.my</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

5) Install jersey 1.1.0-ea maven repository

mvn install:install-file -DgroupId=com.sun.jersey -DartifactId=jersey-server -Dversion=1.1.0-ea -Dpackaging=jar -Dfile=E:\jersey-archive-1.1.0-ea\lib\jersey-server-1.1.0-ea.jar
mvn install:install-file -DgroupId=com.sun.jersey -DartifactId=jersey-core -Dversion=1.1.0-ea -Dpackaging=jar -Dfile=E:\jersey-archive-1.1.0-ea\lib\jersey-core-1.1.0-ea.jar
mvn install:install-file -DgroupId=javax.ws -DartifactId=jsr311 -Dversion=1.1 -Dpackaging=jar -Dfile=E:\jersey-archive-1.1.0-ea\lib\jsr311-api-1.1.jar
mvn install:install-file -DgroupId=org.objectweb -DartifactId=asm -Dversion=3.1 -Dpackaging=jar -Dfile=E:\jersey-archive-1.1.0-ea\lib\asm-3.1.jar

6) Running The Application
cd/C:\tmp\restful\SimpleRESTApp
mvn tomcat:run

7) Testing Your REST Resource
http://localhost:8080/SimpleRESTApp/greeting

Reference:
http://blogs.sun.com/japod/entry/building_simple_jersey_web_app
分享到:
评论

相关推荐

    Build a RESTful Web service using Jersey and Apache Tomcat.

    为了在Apache Tomcat服务器上部署Jersey创建的RESTful服务,开发者需要执行一系列步骤,如将Jersey库添加到项目的依赖中,配置web.xml来声明Jersey提供的Servlet,以及编写相应的资源类和方法。一旦这些步骤正确执行...

    maven +Jersey Restful实现源码

    总结来说,结合 Maven 和 Jersey 可以轻松地创建 RESTful Web 服务。Maven 提供了项目管理和构建的便利,而 Jersey 则简化了 REST 服务的实现。这种组合使得开发者可以快速地构建出符合 RESTful 风格的、可扩展的 ...

    building restful web services with spring 5 2e

    Building RESTful Web Services with Spring 5 – Second Edition: Leverage the power of Spring 5.0, Java SE 9, and Spring Boot 2.0 Find out how to implement the REST architecture to build resilient ...

    Building RESTful Web Services with Go

    Building RESTful Web Services with Go:Initially, SOAP-based web services became more popular with XML. Then, since 2012,REST picked up the pace and gulped SOAP in whole. The rise of a new generation ...

    maven+jersey项目源码

    6. **扩展性**:Jersey 提供了丰富的API和扩展点,可以与其他框架如CDI(Context and Dependency Injection)、JPA(Java Persistence API)等结合使用,构建复杂的Web应用程序。 在给定的"maven+jersey项目源码"中...

    Building RESTful Web services with Go

    The depth, breadth, and ease of use of Go, makes it a breeze for developers to work with it to build robust Web APIs. This book takes you through the design of RESTful web services and leverages a ...

    Building RESTful Web services with Go mobi

    Building RESTful Web services with Go 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    eclipse-maven-jersey demo

    【标题】"eclipse-maven-jersey demo"是一个演示项目,它展示了如何在Eclipse环境中使用Maven构建和管理一个基于Jersey的RESTful Web服务应用。Jersey是Java平台上的一个开源框架,用于实现 Representational State ...

    RESTful Web Services with Dropwizard

    Dropwizard is a Java development framework for RESTful Web Services. It was initially built by Yammer to be used as the base of their backend systems. Dropwizard is production-ready; it encapsulates ...

    Building RESTful Web Services with PHP 7 epub

    Building RESTful Web Services with PHP 7 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Building RESTful Web Services with PHP 7 azw3

    Building RESTful Web Services with PHP 7 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Building RESTful Web Services with Java EE 8

    You will then delve into the details of implementing synchronous RESTful web services and clients with JAX-RS. Next up, you will learn about the specifics of data binding and content marshalling ...

    Building RESTful Web Services with .NET Core [PDF]

    根据提供的文件信息,本书《利用 .NET Core 构建 RESTful Web 服务》是一本全面介绍如何使用 .NET Core 平台开发 RESTful Web 服务的技术书籍。下面将基于本书的目录来概述每一章节的关键知识点。 ### 第一章:入门...

    Java Restful Web 源代码,Java Restful Web 源代码

    Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web...

    Building RESTful Web Services with Spring 5(2nd) epub

    Building RESTful Web Services with Spring 5(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除查看此书详细信息请在美国亚马逊官网搜索此书

    Django restful web services the easiest way to build python

    ### Django RESTful Web Services: The Easiest Way to Build Python #### 一、概述与背景 本书主要聚焦于如何利用Python及其最受欢迎的Web框架Django来构建RESTful Web服务。随着互联网技术的发展,RESTful Web...

    构建Restful Web服务-jersey

    【标题】:“构建Restful Web服务-jersey” 在Web开发中,RESTful API已经成为构建分布式系统和服务之间交互的重要方式。Jersey是Java语言中实现RESTful架构风格接口的一个开源框架,它基于JAX-RS(Java API for ...

    RESTful Web Services with Dropwizard.pdf

    RESTful Web Services with Dropwizard是一本关于使用Dropwizard框架构建RESTful服务的入门教程书籍。Dropwizard是一个开源的Java框架,它用于构建支持RESTful架构的服务。这种架构允许开发者构建可互操作的Web服务...

    eclipse-maven-jersey demo2

    【标题】"eclipse-maven-jersey demo2"是一个示例项目,旨在演示如何在Eclipse集成开发环境中创建一个基于Maven构建的Web工程,并整合Jersey框架来实现RESTful服务。这个教程将帮助开发者了解如何配置Maven、Eclipse...

    使用Maven和Jersey Framework开发REST风格Web Service

    Maven是Java项目管理和综合工具,而Jersey则是Java EE中用于实现RESTful Web服务的官方库。 **一、Maven简介** Maven是一个强大的构建工具,它简化了项目的构建、依赖管理和文档生成。Maven使用一个项目对象模型...

Global site tag (gtag.js) - Google Analytics