2

Trying to add a restful web service call to an existing Spring 3.11 MVC application, but the app has a url mapping in web.xml for spring like this:

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

So everything basically needs to end in .html

I would like for my web service to not have to do that - ideally by using a unique context/subfolder for my web services, such as /restful for example.

But I am not sure how to get it to work...

So if my controller was like this:

@ResponseBody
    @RequestMapping(value="restful/test/{test}",method = RequestMethod.GET)
    public String test(@PathVariable String test) 
    {   
        return "OK"+test;
    }

And I wanted to access it by: localhost/blah/restful/test/text (no .html anywhere, and where blah is the application context) how should I handle the url-pattern in the web.xml -- without interfering with anything already existing in the app?

When I add another url-pattern like this:

<servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/restful/*</url-pattern>
    </servlet-mapping>

And try to access localhost/blah/restful/test/text, I get a 404 error.

The other part of the web-xml for servlet looks like this:

<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

And the view resolver in that spring-mvc.xml file is like this:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
Trant
  • 3,461
  • 6
  • 35
  • 57
  • Possible duplicate of [many url-pattern for the same servlet](http://stackoverflow.com/questions/8995353/many-url-pattern-for-the-same-servlet) – Rafik BELDI Dec 16 '15 at 16:20
  • I realize it's not actually related to trying to get multiple patterns anymore - it's either the pattern syntax itself or something else in this app's config causing the problem – Trant Dec 16 '15 at 16:44
  • it's all your web.xml i think you miss some thing in it – Abdelhak Dec 16 '15 at 16:57

2 Answers2

3

Assuming you're using the default MVC configuration, given

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/restful/*</url-pattern>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

and

@RequestMapping(value="restful/test/{test}", method = RequestMethod.GET)

you should be trying to access localhost/blah/restful/restful/test/text (note the double /restful).

Spring MVC, in requests with a path mapping (see Servlet Specification chapter 12), tries to extract the path segment that was matched by the container (the first /restful in this case) and therefore use the rest /restful/test/text to find an appropriate handler (your @Controller's @RequestMapping annotated method).

I would simply remove the restful part from your @RequestMapping. You'd be left with

@RequestMapping(value="/test/{test}", method = RequestMethod.GET)

which would be able to handle a request to /localhost/blah/restful/test/text.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
-1

Do you have tried this ?

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>/restful/*</url-pattern>
</servlet-mapping>

Or just in a different order like this (because order should matter):

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/restful/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
Selim Ok
  • 1,141
  • 1
  • 7
  • 21