欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  资讯频道

60秒创建RESTful Web Services

程序员文章站 2024-02-24 16:04:53
...

60秒创建RESTful Web Services,这可能吗?我给你展示一下如何快速创建RESTful Web Services。

首先准备好开发环境:

1. NetBeans IDE 6.x, I had the latest 6.5 M1 version downloaded.
2. JDK version 5 or 6
3. GlassFish V2 Application Server

如果不了解REST的话,查看下面链接学习一下先:

    * RESTful Web Services
    * Implementing RESTful Web Services in Java
    * JSR 311: JAX-RS: The JavaTM API for RESTful Web Services

都准备好了?让我们开始吧。

创建一个新项目
Choose File -> New Project. Select Web within the Categories and select Web Application

under Projects and click Next.

60秒创建RESTful Web Services

2.输入项目名HelloWorldRestWS

60秒创建RESTful Web Services

3。在下图中选择GlassFish V2作为server,Java EE 5作为java版本

60秒创建RESTful Web Services

创建web resource

1。右键选择项目,选择New - RESTful Web Services,在下面界面中选择Singleton Pattern

60秒创建RESTful Web Services

2。然后确认输入,不要忘记改变 MIME Type,从application/xml 变成 text/plain

60秒创建RESTful Web Services

3.IDE自动生成的代码如下:

/*
 *  HelloWorldResource
 *
 * Created on July 23, 2008, 10:13 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.stelligent.ws;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.ConsumeMime;

/**
 * REST Web Service
 *
 * @author meerasubbarao
 */

@Path("helloWorld")
public class HelloWorldResource {
    @Context
    private UriInfo context;

    /** Creates a new instance of HelloWorldResource */
    public HelloWorldResource() {
    }

    /**
     * Retrieves representation of an instance of com.stelligent.ws.HelloWorldResource
     * @return an instance of java.lang.String
     */
    @GET
    @ProduceMime("text/plain")
    public String getText() {
        //TODO return proper representation object
        throw new UnsupportedOperationException();
    }

    /**
     * PUT method for updating or creating an instance of HelloWorldResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @ConsumeMime("text/plain")
    public void putText(String content) {
    }
}


测试RESTful Web Services

1。右键点击项目选择Test RESTful Web Services,GlassFish V2 application server启动,应用部署。

60秒创建RESTful Web Services

2.出现错了?
60秒创建RESTful Web Services

3.不用着急,我们可以修复它。
    /**
     * Retrieves representation of an instance of com.stelligent.ws.HelloWorldResource
     * @return an instance of java.lang.String
     */
    @GET
    @ProduceMime("text/plain")
    public String getText() {
        //TODO return proper representation object
        throw new UnsupportedOperationException();
    }


修改为:

/**
     * Retrieves representation of an instance of com.stelligent.ws.HelloWorldResource
     * @return an instance of java.lang.String
     */
    @GET
    @ProduceMime("text/plain")
    public String sayHello{
        //TODO return proper representation object
        return "Hello World from REST web services generated in 60 seconds";
    }

4。运行应用:

60秒创建RESTful Web Services

成功了,很简单吧!

来自:http://www.testearly.com/2008/07/23/restful-web-services-in-60-seconds/
相关标签: Web