0

Currently I have the following web.xml config:

<servlet>
    <servlet-name>Website</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Website</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Server</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Server</servlet-name>
    <url-pattern>/server/</url-pattern>
</servlet-mapping>

As for two example controllers:

@Controller
public class ApiRequests {
    protected final Logger logger = Logger.getLogger(ApiRequests.class);

    @RequestMapping(value = "api.json", produces = {"application/json"}, method = RequestMethod.GET)
    @ResponseBody
    public String handleActionJson(final HttpServletRequest request){
        return "{test: 'blaat'}";
    }
}

And

@Controller
@RequestMapping("/")
public class IndexController {
    @RequestMapping("*.html")
    public void showIndex(){

    }
}

Now my problem is that when I try to call the /server/api.json url, the server won't give the json response, instead it gives me the following error:

PageNotFound:1108 - No mapping found for HTTP request with URI [/WorldDefense/server/api.json] in DispatcherServlet with name 'Website'

Which basically means its trying to search for /server/api.json in the Website servlet instead of the Server serlvet, as for my question: would it be possible to get this setup to work? (So /server/ to map to the Server servlet and all the other url combinations to the Website servlet)

Edit 1 I updated the code to reflect the changes as suggested by Costi Ciudatu but it still doesn't work. I removed the @RequestMapping("/server") and now only have the @RequestMapping at the handleActionJson method. Both resulting in these errors:

10:57:26,046  WARN PageNotFound:1108 - No mapping found for HTTP request with URI [/WorldDefense/server/server/api.json] in DispatcherServlet with name 'Website'
10:57:40,509  WARN PageNotFound:1108 - No mapping found for HTTP request with URI [/WorldDefense/server/api.json] in DispatcherServlet with name 'Website'

Mappings according to the tomcat log:

Server-servlet

11:03:49,655  INFO RequestMappingHandlerMapping:178 - Mapped "{[/api.json],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public java.lang.String com.world2.worlddefense.server.controllers.ApiRequests.handleActionJson(javax.servlet.http.HttpServletRequest)
11:03:50,125  INFO RequestMappingHandlerMapping:178 - Mapped "{[/api.json],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public java.lang.String com.world2.worlddefense.server.controllers.ApiRequests.handleActionJson(javax.servlet.http.HttpServletRequest)

Website-servlet

11:03:50,380  INFO RequestMappingHandlerMapping:178 - Mapped "{[//*.html],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.world2.worlddefense.website.controllers.IndexController.showIndex()
11:03:50,381  INFO RequestMappingHandlerMapping:178 - Mapped "{[/login],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.world2.worlddefense.website.controllers.TempTestController.showTest()
Jaap Oudejans
  • 708
  • 2
  • 9
  • 25

1 Answers1

1

You can define multiple DispatcherServlets in the same web.xml and map them as you wish. However, the servlet mapping should not be reflected in the @RequestMapping of your controllers. Your controllers always map to paths relative to the servlet mapping; in your case, I think you could get that JSON response if you tried /WorldDefence/server/server/api.json...

If you want different controllers to be associated with the two dispatchers, you only need to make sure that the proper controllers are loaded in the corresponding application context: Website-servlet.xml and Server-servlet.xml in case you stick with this convention.

Long story short, your mapping for ApiRequests should be '/', not '/server' and that controller should only be included in the "Server" dispatcher context.

As a side note, since 3.2.x, the .json extension is handled by the ContentNegotiationManager, so your controller should return some domain object which will be transparently marshaled by the Spring HttpMessageConverter mechanism and will thus be able to produce media types other than JSON with no effort.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • See updated question, it still gives me problems. Also, the controllers of server and website are loaded in their own application context (Website-servlet.xml and Server-servlet.xml) – Jaap Oudejans Mar 19 '13 at 10:06