Jersey框架Getting Started

    实践是检验真理的唯一标准,上篇文章主要介绍了RESTful service resources思想创建服务,这篇我们用其思想和Jersey框架创建一个项目(本文是针对在eclipse中的项目来进行的,maven和ant developers的用户搭建环境可参照https://jersey.java.net/nonav/documentation/latest/jax-rs.html网页):

    有关jersey的介绍,上章已经介绍过了,这里再重复一下:Jersey是由SUN提供的JAX-RS实现参考,对JAX-RS支持的最为充分和快速,基本上所有的JAX-RS的新特性都会在Jersey里第一个体现出来。

    废话不说,直接动手:

    第一步,create project:

    eclipse_j2ee中file->new project->dynamic web project,命名为RestDemo.

    第二步,添加库文件

    第三步,在你的工程中创建资源类:

package com.frand.RestDemo;

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

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

    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String getClichedMessage() {
         // Return some cliched textual content
         return "Hello World";
     }
 }

     第四步,在你的项目中创建资源管理类:

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import javax.ws.rs.ApplicationPath;

import com.frand.RestDemo.HelloWorldResource;

/**
 *
 * @author frand
 */
public class Main extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<Class<?>>();
        // register root resource
        classes.add(HelloWorldResource.class);
        return classes;
    }
}

     第五步,在web.xml文件中配置环境参数:

<?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>RestDemo</display-name>
  
  <servlet>
        <servlet-name>com.frand.Main</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.frand.Main</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.frand.Main</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
  <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>
</web-app>

     第六步,右击项目,run as->run on server;就会在内置浏览器中看到Hello World两个单词。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Getting Started with Angular - Second Edition by Minko Gechev English | 6 Apr. 2017 | ISBN: 1787125270 | 308 Pages | EPUB/PDF (conv) | 7.7 MB Key Features Get up to date with the final changes to Angular 2, including the improvements to directives, change detection, dependency injection, router, and more Understand Angular 2's new component-based architecture Start using TypeScript to supercharge your Angular 2 applications Book Description Angular is the modern framework you need to build performant and robust web applications. This book is the quickest way to get to grips with Angular 2 and will help you transition to the brave new world of Angular 2. It starts with an overview putting the changes of the framework in context with version 1.x. After that, you will be taken on a TypeScript crash-course so you can take advantage of Angular 2 in its native, statically-typed environment. We'll look at the new change-detection method in detail, how Directives and Components have changed how you create websites with Angular, the new Angular 2 router, and much more. By the end of the book, you'll be ready to start building quick and efficient Angular 2 applications that take advantage of all the new features on offer. What you will learn Understand the changes made from Angular 1.x with side-by-side code samples to help demystify the Angular 2 learning curve Start working with Angular 2's new method of implementing directives Use TypeScript to write modern, powerful Angular 2 applications Dig in to the change-detection method, and other architectural changes to make sure you know what's going on under the hood of Angular 2 Get to work with the new router in Angular 2 Use the new features of Angular 2 including pipes, or the updated features such as forms, services, and the DI module Master server-side rendering in Angular 2 to keep your new applications SEO-friendly

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值