5

Tomcat 7、Apache Wink、および Jackson JSON Processor を使用して単純な 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>

ここで、/* を代わりに 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;
    }

}

そして、私の従業員クラスには、次のものがあります。

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;
    }
}

何か案は?私はしばらくの間、これに頭を悩ませてきました

4

2 に答える 2

5

サーブレット (web.xml 内) の url-pattern パラメータは、Employee クラスで指定したパスとは無関係です。

<servlet-mapping>
    <servlet-name>ExampleServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

サーブレットが /services/ サブパスでリッスンすることを意味します。

@Path("/services/employee")

REST アプリケーションが /services/employee "sub-sub-path" をリッスンすることを意味します。

したがって、Web サービスは localhost:8080/example/services/services/employee (url-pattern と @Path アノテーションの連結) で公開されます。

上記の URL パターンを使用して localhost:8080/example/services/employee で公開する場合は、Employee クラスを次のように変更する必要があります。

@Path("employee")
于 2012-11-12T20:00:16.540 に答える
1

どこ/services/*に行くと思いますか?Web アプリは、Web アプリケーションがサービスを提供する URL パターンを示します。これにより、アプリケーション サーバー (JBoss、GlassFish など)/services/foo/bar/whateverが Web アプリケーションにルーティングされます。Employeeクラスはリクエストに応答して呼び出されるため、そのリクエストを行う/services/employeeことができます。他に何も登録されてい/services/*ないため、404 または 400 応答が返されます。は Web アプリケーションに登録されているため/services/*、400 が期待されます。

于 2012-11-12T19:43:38.750 に答える