Last Modified 2019.10.22

RESTful URL

A REST does not store user sessions in an https environment.
The request URL for REST programming should be made to mean the location of the resource.
Even if it's not a REST program, making the request URL a RESTful URL makes a program better program.
This article explains how to convert the request URL of SpringBbs bulletin board program to RESTful URLs.

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

The following is the request URL of the current bulletin board. --Assume you did not 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 the result of modifying the bulletin board request URL to match the RESTful URL.

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=

New Form and Modify Form request URLs are not RESTful URLs.
This decision was made to minimize modifications to the bulletin board program.

List

In /chat?page=1&searchWord=, chat is the board code.
Do you think '/chat/1?searchWord=' is possible? --The page number is included in the URL--
However, do not include the page number in the URL.
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='.
seachWord is an optional parameter. do not include optional parameters in the URL.
The page value changes depending on the searchWord value.
Do not include the parameter in the URL that is affected by the value of optional parameter.

Modify the list method declaration of board controller that handles 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 board controller that handles 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.
To use this request URL, List page must contain New Form.
However, this change makes bean validation difficult.
So I decided to use New Form page as it is.
It doesn't fit a RESTful URL, but I determined the new form request URL as follows:
/chat/new?page=1&searchWord=

Modify the writeForm method declaration of board controller that handles the changed 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

Modify the write method declaration of board controller that handles the changed 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.
To use this request URL, Detailed View page must contain Modify Form.
However, this change makes bean validation difficult.
So I decided to use Modify Form page as it is.
It doesn't fit a RESTful URL, but I determined the Modify Form request URL as follows:
/chat/54/edit?page=1&searchWord=

Modify the modifyForm method declaration of board controller that handles the changed 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=

Modify the modify method declaration of board controller that handles the changed 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=

Modify the deleteArticle method declaration of board controller that handles the changed 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

You need to modify most JSPs.
Modify the method values in the form elements.
Modify the lines that create request strings to match the changed URLs.
consider this and modify the JSPs. Since boardCd and articleNo are no longer passed as request parameters, consider this and modify JSPs.

Related Articles