Search code examples
java-ee-6jax-rserrai

javax.ws.rs.core.PathSegment and Errai


Errai has support for JAX-RS, although when I tried to use it with the supplied Maven archetype GWT complained with the error:

No source code is available for type javax.ws.rs.core.PathSegment; did you forget to inherit a required module?

The PathSegment interface appears to be part of the official Java EE standard, so why is this class not available?


Solution

  • A solution is to create the javax.ws.rs.core package in your GWT project, and include the interfaces for yourself. These interfaces are MultivaluedMap:

    package javax.ws.rs.core;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * A map of key-values pairs. Each key can have zero or more values.
     * 
     */
    public interface MultivaluedMap<K, V> extends Map<K, List<V>> {
    
        /**
         * Set the key's value to be a one item list consisting of the supplied value.
         * Any existing values will be replaced.
         * 
         * @param key the key
         * @param value the single value of the key
         */
        void putSingle(K key, V value);
    
        /**
         * Add a value to the current list of values for the supplied key.
         * @param key the key 
         * @param value the value to be added.
         */
        void add(K key, V value);
    
        /**
         * A shortcut to get the first value of the supplied key.
         * @param key the key
         * @return the first value for the specified key or null if the key is
         * not in the map.
         */
        V getFirst(K key);
    
    }
    

    PathSegment:

    package javax.ws.rs.core;
    
    /**
     * Represents a URI path segment and any associated matrix parameters. When an
     * instance of this type is injected with {@link javax.ws.rs.PathParam}, the
     * value of the annotation identifies which path segment is selected and the
     * presence of an {@link javax.ws.rs.Encoded} annotation will result in an
     * instance that supplies the path and matrix parameter values in
     * URI encoded form.
     *
     * @see UriInfo#getPathSegments
     * @see javax.ws.rs.PathParam
     */
    public interface PathSegment
    {
    
       /**
        * Get the path segment.
        * <p/>
        *
        * @return the path segment
        */
       String getPath();
    
       /**
        * Get a map of the matrix parameters associated with the path segment.
        * The map keys are the names of the matrix parameters with any
        * percent-escaped octets decoded.
        *
        * @return the map of matrix parameters
        * @see <a href="http://www.w3.org/DesignIssues/MatrixURIs.html">Matrix URIs</a>
        */
       MultivaluedMap<String, String> getMatrixParameters();
    
    }
    

    You'll need a GWT module file (in the javax.ws.rs package):

    <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.6//EN"
            "http://google-web-toolkit.googlecode.com/svn/releases/1.6/distro-source/core/src/gwt-module.dtd">
    <module rename-to="App">
        <source path="core"/> 
    </module>
    

    And you'll need to add an inherits reference to you application GWT module:

    <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.6//EN"
            "http://google-web-toolkit.googlecode.com/svn/releases/1.6/distro-source/core/src/gwt-module.dtd">
    <module rename-to="App">
        <inherits name='com.google.gwt.user.User'/>
        <inherits name="org.jboss.errai.common.ErraiCommon"/>
        <inherits name="org.jboss.errai.ioc.Container"/>
    
        <inherits name="org.jboss.errai.enterprise.Jaxrs"/> 
        <inherits name="com.redhat.topicindex.rest"/>
        <inherits name="javax.ws.rs.core"/>
    </module>