Last Modified 2022.2.8

RESTful URL

REST does not store user sessions in an https environment.

You should make the request URL for REST programming --it is called RESTful URL-- to mean the location of the resource.

RESTful URL Example: Address Book

The RESTful URL for the Address Book application is:

List:			GET /address
Address with ID 1:	GET /address/1 
Registration:		POST /address
Modify:			PUT /address/1
Modify specific field:	PATCH /address/1
Delete:			DELETE /address/1

Even if it's not a REST program, making the request URL a RESTful URL makes it a better program.

This article explains how to convert the request URL of the SpringBbs bulletin board program to RESTful URLs.

The following is the request URL of the current bulletin board. Assume you didn't search.

List:		GET /list?boardCd=chat&page=1&searchWord=
Detailed View:	GET /view?boardCd=chat&page=1&searchWord=
New Form:	GET /write?boardCd=chat&page=1&searchWord=
New:		POST /write?boardCd=chat&page=1
Modify Form:	GET /modify?boardCd=chat&page=1&searchWord=
Modify:		POST /modify?boardCd=chat&page=1&searchWord=
Delete:		POST /del?boardCd=chat&page=1&searchWord=

The following is converting bulletin board request URLs to RESTful URLs.

List:		GET /chat?page=1&searchWord=
Detailed View:	GET /chat/54?page=1&searchWord=
New Form:	GET /chat/new?page=1&searchWord=
New:		POST /chat?page=1
Modify Form:	GET /chat/54/edit?page=1&searchWord=
Modify:		PUT /chat/54?page=1&searchWord=
Delete:		DELETE /chat/54?page=1&searchWord=

In /chat?page=1&searchWord=, chat is the board code. Do you think '/chat/1?searchWord=' is better? The answer is no. There are two reasons for this. First, according to Clean URL, 1 in '/chat/1?searchWord=' means the first post of the chat bulletin board, not the page number. --Clean URL is similar in concept to RESFful URL-- Second, if you don't search, you can use '/chat?page=1' instead of '/chat?page=1&searchWord='. That means seachWord is an optional parameter. You better not add optional parameters to the URL. A page value changes depending on a searchWord value. You better not add the parameter that is affected by the value of the optional parameter to the URL.

List

Modify the list method declaration of board controller to handle the changed request as follows:

@RequestMapping(value = "{boardCd}", method = RequestMethod.GET)
public String list(..., @PathVariable String boardCd, ...)

A recent Spring can shorten this annotation simply to:

@GetMapping("{boardCd}")
public String list(..., @PathVariable String boardCd, ...)

In @RequestMapping(value = "{boardCd}", method = RequestMethod.GET) and @GetMapping("{boardCd}"), {boardCd} does not literally mean. If the list method has a parameter declared as @PathVariable String boardCd, in a '/chat?page=1&searchWord=' request, 'chat' is assigned to the boardCd parameter.

Detailed View

In '/chat/54?page=1&searchWord=', 54 is the unique number of the post. Modify the view method declaration of the board controller to handle the changed request as follows:

@RequestMapping(value = "{boardCd}/{articleNo}", method = RequestMethod.GET)
public String view (..., @PathVariable String boardCd, @PathVariable Integer articleNo, ...)

A recent Spring can shorten this annotation simply to:

@GetMapping("{boardCd}/{articleNo}")
public String view (..., @PathVariable String boardCd, @PathVariable Integer articleNo, ...)

When '/chat/54?page=1&searchWord=' request comes in, 'chat' is assigned to the parameter boardCd and '54' is assigned to the parameter articleNo.

New Form

'/chat?page=1&searchWord=' is a RESTful URL for New Form. But you must add a form for adding a post on the list page to use this URL. This change makes bean validation difficult.

It doesn't fit a RESTful URL, but let's use the new form request URL as follows:

/chat/new?page=1&searchWord=

Modify the writeForm method declaration of the board controller to handle the new form request as follows:

@RequestMapping(value = "{boardCd}/new", method = RequestMethod.GET)
public String writeForm(@PathVariable String boardCd, ...)

A recent Spring can shorten this annotation simply to:

@GetMapping("{boardCd}/new")
public String writeForm(@PathVariable String boardCd, ...)

New

'POST /chat?page=1' is a RESTful URL for the adding new post request. Modify the write method declaration of the board controller to handle the adding the new post request as follows:

@RequestMapping(value = "{boardCd}", method = RequestMethod.POST)
public String write(..., @PathVariable String boardCd, ...)

A recent Spring can shorten this annotation simply to:

@PostMapping("{boardCd}")
public String write(..., @PathVariable String boardCd, ...)

Modify Form

'/chat/54?page=1&searchWord=' is a RESTful URL for the modify form. But you must add the form for modifying the post to the detailed view page to use the above URL. This change makes bean validation difficult.

It doesn't fit a RESTful URL, but let's use the Modify Form request URL as follows:

/chat/54/edit?page=1&searchWord=

Modify the modifyForm method declaration of the board controller to handle the Modify Form request as follows:

@RequestMapping(value = "{boardCd}/{articleNo}/edit", method = RequestMethod.GET)
public String modifyForm(..., @PathVariable String boardCd, ...)

A recent Spring can shorten this annotation simply to:

@GetMapping("{boardCd}/{articleNo}/edit")
public String modifyForm(..., @PathVariable String boardCd, ...)

Modify

'PUT /chat/54?page=1&searchWord=' is a RESTful URL for the modifying post URL. Modify the modify method declaration of the board controller to handle this request as follows:

@RequestMapping(value = "{boardCd}/{articleNo}", method = RequestMethod.PUT)
public String modify(..., @PathVariable String boardCd, @PathVariable Integer articleNo, ...)

A recent Spring can shorten this annotation simply to:

@PutMapping("{boardCd}/{articleNo}")
public String modify(..., @PathVariable String boardCd, @PathVariable Integer articleNo, ...)

Delete

'DELETE /chat/54?page=1&searchWord=' is a RESTful URL for the deleting a post URL. Modify the deleteArticle method declaration of the board controller to handle this request as follows:

@RequestMapping(value = "{boardCd}/{articleNo}", method = RequestMethod.DELETE)
public String deleteArticle(@PathVariable String boardCd, @PathVariable Integer articleNo, ...)

A recent Spring can shorten this annotation simply to:

@DeleteMapping("{boardCd}/{articleNo}")
public String deleteArticle(@PathVariable String boardCd, @PathVariable Integer articleNo, ...)

Modify JSPs

Modify JSPs by referring to the followings:

  • Confirm method values in form elements in JSPs.
  • Confirm the lines to build request string in JSPs.
  • Remove the boardCd and articleNo in JSPs because these are no longer request parameters.
Related Articles