0

I am writing spring mvc application.

In my application I have web pages as well as rest web services to handle ajax call.

I have done below entry in web.xml

<servlet>
    <servlet-name>myapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring_myapp-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

Should I map my rest url with same servlet like

<servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Or should I make new servlet entry for rest.

I have done required entries in pom.xml for "org.codehaus.jackson" and also I have made required entries in my spring_myapp-servlet.xml.

For html page I am using below code in my controller

@RequestMapping(value = "/htmlUrl")
public ModelAndView ModifyValiodation(HttpServletRequest request) {
     // my code
}

For rest service I am using

@RequestMapping(value = "/restUrl")
public @ResponseBody Map<String, String> restUrl(HttpServletRequest request) {
     // my code
}

If I am using only one servlet for two url mapping, then total 4 url will be made.

  1. myapp/htmlUrl.html
  2. myapp/restUrl.html
  3. myapp/rest/htmlUrl
  4. myapp/rest/restUrl

If I am using two different servlet with individual dispacherServlet then will i have to make entry of every component and service of spring in both the servlet.xml?

Please point out the solution for exposing rest web service.

Thanks!

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • 1
    No you don't and no you don't have to duplicate anything. If you want to have 2 servlets migrate the common stuff to a general application context that is loaded by the `ContextLoaderListener` that is then a parent context shared amongst all `DispatcherServlets`. Although you could use 2 servlets you can also simply map the controllers to `/rest/restUrl`. – M. Deinum Oct 15 '14 at 06:12
  • 1
    The dispatcher expects to be mapped to `/`. Anything else is for special cases. You should map the dispatcher to `/` and let it deal with _all_ requests. Also, stop using XML; if you are just learning Spring then there is no reason to learn obsolete information. With modern Java you can also do away with `web.xml`. – Boris the Spider Oct 15 '14 at 06:12
  • Thanks for the reply. As I want to append .html to my url, so I have mapped it to *.html. Is there any way in spring which will append .html, so that I can remove it from web.xml mapping. – Naman Gala Oct 15 '14 at 06:16
  • @BoristheSpider can you please give me some more info about how to do it with web.xml and not using xml – Naman Gala Oct 15 '14 at 07:01
  • @M.Deinum and @BoristheSpider thanks for the help. I have got my solution from your answers. Now I am using `/` for DispatcherServlet and in controllers I am using /rest/restUrl. Thanks alot, I had given quite a long time to search for it. @BoristheSpider, still it would be great if you could provide me some link or info about how to do it with web.xml and not using xml – Naman Gala Oct 15 '14 at 07:55

2 Answers2

0

use

<servlet-mapping>
 <servlet-name>myapp</servlet-name>
 <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

If you use two DispatcherServlet entries , it will load two ApplicationContext Objects in your application. Since you are using spring mvc to handle all the requests to your app, you should be fine with this configuration. Any request url that ends with .html or any urls that contains /rest/ will be handled by spring.

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
  • Thanks for the reply. As I am using @RequestMapping, it will map all urls to both servlet-mapping. How do I differentiate and make it to map the specific urls to /rest – Naman Gala Oct 15 '14 at 06:23
0

It is up to you to design the server side of the infrastructure.

Neither the RESTful specifications have any instructions for doing this nor the Servlet specifications enforce anything on this.

On the Applications design I think it is better idea to keep two different servlets to handle different URLs because over time the classes will become complex and long. These to may be used as front controllers and may have common logic class in the backend.

Pankaj Jangid
  • 524
  • 3
  • 18