无法使用/ *以外的url模式进行REST调用

gor*_*ysc 5 rest url-pattern

我正在尝试使用Tomcat 7,Apache Wink和Jackson JSON处理器来做一个简单的REST Web应用程序,但是似乎遇到了麻烦。如果查看我的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>Example Web Application</display-name>
    <servlet>
        <servlet-name>ExampleServlet</servlet-name>
        <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.dummy.example.server.ExampleApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ExampleServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)

现在,如果我用/ *代替URL模式,则REST调用有效,但是当我使用/ services / *时,它将失败。

在我的ExampleApplication中,我看到:

package com.dummy.example.server;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.codehaus.jackson.map.AnnotationIntrospector;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;
import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;

public class ExampleApplication extends Application {

    /**
     * Get the list of service classes provided by this JAX-RS application
     */
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> serviceClasses = new HashSet<Class<?>>();
        serviceClasses.add(com.dummy.example.server.services.Employee.class);
        return serviceClasses;
    }

    @SuppressWarnings("deprecation")
    @Override
    public Set<Object> getSingletons() {
        Set<Object> s = new HashSet<Object>();

        // Register the Jackson provider for JSON

        // Make (de)serializer use a subset of JAXB and (afterwards) Jackson annotations
        // See http://wiki.fasterxml.com/JacksonJAXBAnnotations for more information
        ObjectMapper mapper = new ObjectMapper();
        AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
        AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
        AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
        mapper.getDeserializationConfig().setAnnotationIntrospector(pair);
        mapper.getSerializationConfig().setAnnotationIntrospector(pair);

        // Set up the provider
        JacksonJaxbJsonProvider jaxbProvider = new JacksonJaxbJsonProvider();
        jaxbProvider.setMapper(mapper);

        s.add(jaxbProvider);
        return s;
    }

}
Run Code Online (Sandbox Code Playgroud)

在我的Employee班上,我有:

package com.dummy.example.server.services;

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

import org.json.simple.JSONObject;

@Path("/services/employee")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public class Employee {
    @GET
    public JSONObject get() {
        JSONObject json = new JSONObject();
        json.put("Name", "Example");
        return json;
    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我已经为此撞了一段时间

Chr*_*e L 5

servlet(在web.xml中)的url-pattern参数独立于您在Employee类中指定的路径。

<servlet-mapping>
    <servlet-name>ExampleServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

表示您的servlet侦听/ services /子路径。

@Path("/services/employee")
Run Code Online (Sandbox Code Playgroud)

意味着您的REST应用程序侦听/ services / employee的“ sub-sub-path”。

因此,您的Web服务位于localhost:8080 / example / services / services / employee(url-pattern和@Path批注的串联)。

如果要使用提到的url-pattern在localhost:8080 / example / services / employee中公开它,则需要更改Employee类需要具有:

@Path("employee")
Run Code Online (Sandbox Code Playgroud)