0

こんにちは、春の新入社員です。Spring Web API を開発していますが、正規表現で URL を解析する際に問題があります。私はすでに次の投稿を見ました:

http://stackoverflow.com/questions/7841770/optional-path-variables-in-spring-mvc-requestmapping-uritemplate
http://stackoverflow.com/questions/12516969/spring-mvc-getting-pathvariables-containing-dots-and-slashes
http://stackoverflow.com/questions/8998419/requestmapping-annotation-in-spring-mvc

しかし、私の問題に対する解決策はまだ見つかりません。すべてのリクエストが 1 つのメソッドにマップされるようにしたいのですが、URL の長さは可変で、パラメーターの数も可変です。スラッシュ /: までではなく、変数 pathValue を使用して URL 全体をキャプチャしたいと思います。

@RequestMapping(値 = "{パス値}"、メソッド = RequestMethod.GET)

Spring でテストしたすべての正規表現は、スラッシュ (/......./) の間のコンテンツをキャプチャし、残りの URL を考慮しません。

要点は、単一のメソッドで URL を解析したいということです。これは、すべてのリクエストがそのメソッドにマップされることを意味します。春にこれを達成する方法はありますか?

あなたの助けとアドバイスに感謝します。

4

1 に答える 1

0

すべてのリクエストを 1 つのハンドラーにディスパッチする場合は、Spring メソッドのディスパッチャーはまったく必要ありません。

代わりに、独自の Request ハンドラーを持つことができます

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="urlMap">
         <map>
              <entry key="/**" value="myCatchAllResourceHandler" />
         </map>
     </property>
     <property name="order" value="100000" />       
</bean>

<bean id="myCatchAllResourceHandler" name="myCatchAllResourceHandler"
      class="MyCatchAllResourceHandler">
</bean>

独自の Request ハンドラを実装する必要があります

public class MyCatchAllResourceHandler extends HttpRequestHandler() {

    /**
     * Process the given request, generating a response.
     * @param request current HTTP request
     * @param response current HTTP response
     * @throws ServletException in case of general errors
     * @throws IOException in case of I/O errors
     */
    void handleRequest(HttpServletRequest request, HttpServletResponse response)
                   throws ServletException, IOException;
         System.out.println("I get invoked");       
    }
}

しかし、正直なところ、これはすべての Spring MVC を捨てるようなものです!

于 2013-03-25T10:15:39.340 に答える