简体   繁体   中英

deploying and testing rest-webservice with eclipse

I have used eclipse + glassfish to deploy my first web-service from eclipse. 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:

<?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.

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

I also wrote a simple client which connects to the service throught java.io.URL, but that one returns:

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?

I guess your class extends javax.ws.rs.core.Application . In addition it should be annotated with @javax.ws.rs.ApplicationPath("your_rest_path") .

With this annotation you don't need the servlet in the web.xml . GlassFish will be able to pick it up automatically.

You must also add all your JAX-RS Resource classes to the Set returned by getClasses() .

I have no experience with Glassfish but v3 seems to be JAX-RS aware.

This means a sub-class of javax.ws.rs.core.Application is not needed.

Just deploy your JAX-RS annotated class with a web.xml as follows

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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