简体   繁体   English

使用eclipse部署和测试rest-webservice

[英]deploying and testing rest-webservice with eclipse

I have used eclipse + glassfish to deploy my first web-service from eclipse. 我使用eclipse + glassfish从eclipse部署我的第一个web服务。 I have the following class: 我有以下课程:

package com.restfully.shop.services;

import javax.ws.rs.core.*;
import java.util.*;

public class ShoppingApplication extends Application {

    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> empty = new HashSet<Class<?>>();

    public ShoppingApplication() {
        singletons.add(new CustomerResource());
    }

    @Override
    public Set<Class<?>> getClasses() {
        return empty;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }


}

and the following web.xml: 和以下web.xml:

<?xml version="1.0" ?>
<web-app>
 <servlet>
        <servlet-name>Rest</servlet-name>
        <servlet-class>com.restfully.shop.services.ShoppingApplication</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Rest</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

As long as I read that jersey is already included with the glassfish - id didnt put anything into the lib folder of the web project. 只要我读到球衣已经包含在glassfish中 - id没有把任何东西放到web项目的lib文件夹中。

but hten when I start the app in the browser: 但是当我在浏览器中启动应用程序时:

Exception

javax.servlet.ServletException: PWC1403: Class com.restfully.shop.services.ShoppingApplication is not a Servlet
root cause

java.lang.ClassCastException: com.restfully.shop.services.ShoppingApplication cannot be cast to javax.servlet.Servlet

The glassfish version is 3.1.1 glassfish版本是3.1.1

I also wrote a simple client which connects to the service throught java.io.URL, but that one returns: 我还写了一个简单的客户端,通过java.io.URL连接到服务,但是那个返回:

404
Location:null

What is wrong with the service and how can I fix it? 该服务有什么问题,我该如何解决? + would be very appreciated - how a services tested??? +将非常感激 - 如何测试服务??? Is it always like that - writing a test class which uses those URL connections or are there any standard ways to test web services? 总是这样 - 编写一个使用这些URL连接的测试类,或者有任何标准方法来测试Web服务?

I guess your class extends javax.ws.rs.core.Application . 我猜你的类扩展了javax.ws.rs.core.Application In addition it should be annotated with @javax.ws.rs.ApplicationPath("your_rest_path") . 此外,它应该使用@javax.ws.rs.ApplicationPath("your_rest_path")进行注释。

With this annotation you don't need the servlet in the web.xml . 使用此批注,您不需要web.xmlservlet GlassFish will be able to pick it up automatically. GlassFish将能够自动拾取它。

You must also add all your JAX-RS Resource classes to the Set returned by getClasses() . 您还必须将所有JAX-RS资源类添加到getClasses()返回的Set

I have no experience with Glassfish but v3 seems to be JAX-RS aware. 我没有Glassfish的经验,但v3似乎是JAX-RS意识到的。

This means a sub-class of javax.ws.rs.core.Application is not needed. 这意味着不需要javax.ws.rs.core.Application的子类。

Just deploy your JAX-RS annotated class with a web.xml as follows 只需使用web.xml部署JAX-RS带注释的类,如下所示

<servlet>  
    <servlet-name>ServletAdaptor</servlet-name>  
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  

<servlet-mapping>  
    <servlet-name>ServletAdaptor</servlet-name>  
    <url-pattern>/resources/*</url-pattern>  
</servlet-mapping>

Your REST application can subsequently be accessed by adding /resources/ to the context root. 随后可以通过将/ resources /添加到上下文根来访问您的REST应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM