Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringMVC - dispatcher servler's url pattern style

I am trying to build a restful style API, using springMVC.

When config the url-pattern for springMVC's DispatcherServlet, there seems to have 2 choice, and I need some advice.

Choice A:
config pattern as: <url-pattern>*.action</url-pattern>
and action use path like @RequestMapping("/role/add.action")

Choice B:
config pattern as: <url-pattern>/api/*</url-pattern>
and action use path like @RequestMapping("/api/role/add")

I prefer to use a style that has no suffix, but in that case I need do add a sub path.

But I am not sure which is more proper to use in a project that serve as a backend to provide restful API, with browser / IOS / Android as its client.


There might be a choice C, but I am not sure:

config pattern as: <url-pattern>/*</url-pattern>
and action use path like @RequestMapping("/role/add")

In this case built-in servlet will be override, e.g jsp won't work normally.
But I don't have any jsp, and also, static resource like html / js / css / image / document / music / video are all put on another port or server served by nginx, request to tomcat only provide ajax service via json data.
So in this case is it proper to use choice C, or it has some bad side-effects?

like image 650
user218867 Avatar asked Apr 22 '15 09:04

user218867


People also ask

Which design pattern is used in dispatcher Servlet?

The pattern-savvy reader will recognize that the DispatcherServlet is an expression of the “Front Controller” design pattern (this is a pattern that Spring Web MVC shares with many other leading web frameworks).

What is URL pattern in Servlet mapping?

The url-pattern element of a servlet-mapping or a filter-mapping associates a filter or servlet with a set of URLs. When a request arrives, the container uses a simple procedure for matching the URL in the request with a url-pattern in the web. xml file. Section 4.7.

What is DispatcherServlet and ContextLoaderListener?

Basic. The task of the DispatcherServlet is to send request to the specific Spring MVC controller. ContextLoaderListener reads the Spring configuration file (with value given against contextConfigLocation in web.xml ), parses it and loads the singleton bean defined in that config file.

What pattern does the DispatcherServlet from Spring MVC framework implement?

Spring MVC Framework follows the Model-View-Controller design pattern. It is used to develop web applications. It works around DispatcherServlet. DispatcherServlet handles all the HTTP requests and responses.


2 Answers

if your goal is restful api my choice is the second one since you identify the resource in the url; say you must manage a role resource you should have some mapping like these ones:

@RequestMapping("/api/role" method = RequestMethod.POST)

to insert a new role (may be the api does not allow this)

@RequestMapping("/api/role/{roleId}" method = RequestMethod.PUT)

to update an existing role

@RequestMapping("/api/role/{roleId}" method = RequestMethod.DELETE)

to delete a role

@RequestMapping("/api/role" method = RequestMethod.GET)

to retrieve roles (you may implement some filters via query string)

The same applies for other resources (User, etc) the naming schema is the same.

I vould avoid option C since I think it's best to have a dedicated mapping for the api if you app also ship a web interface that does not use the api

like image 136
Giovanni Avatar answered Oct 14 '22 07:10

Giovanni


I will go with the Choice B for RESTful services, consider performing CRUD operations using REST. And you can map the url-pattern as ,

config pattern as: <url-pattern>/api/*</url-pattern>

So to perform add , you can just make sure that you post the JSON object from the page and have a url like /api/add

And in case of delete , you can simply follow the same . Consider you are going to delete a object from the list using its id . You can simply make it out as,

/api/delete/${id}

And handle it like ,

@RequestMapping(value="/{id}", method=RequestMethod.GET)
like image 42
Santhosh Avatar answered Oct 14 '22 09:10

Santhosh