The examples on this page are generated by the app’s tests with Spring REST Docs, so they always match the code (see GameControllerDocsTest). Source: github.com/randomvlad/TicTacToe.

Overview

The app renders HTML server-side with Thymeleaf, so these endpoints are browser routes rather than a JSON API.

  • Base URL: all paths are under the /tictactoe context path, e.g. http://localhost:8080/tictactoe/.

  • Authentication: every endpoint except /login and the static assets (/css/*, /images/*, /js/*) requires a logged-in user. Requests without a session are redirected to /login. Log in with rick or morty and password pickle.

  • CSRF: every POST must include a _csrf form parameter that matches the session. Thymeleaf adds it as a hidden field to the app’s forms.

  • Post/Redirect/Get: the POST game endpoints change state and then redirect to GET /, so refreshing the page never resubmits a move.

Game

Show Game Page

GET / renders the user’s last game. If the user has no game yet, a new one is created with the player moving first. The response is the HTML game page.

Example Request

$ curl 'http://localhost:8080/tictactoe/' -i -X GET \
    --cookie 'JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A'
GET /tictactoe/ HTTP/1.1
Host: localhost:8080
Cookie: JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A

Start New Game

POST /new-game starts a new game and replaces the user’s previous one. If the computer moves first, it always takes the center tile 1-1.

Form Parameters

Parameter Description

_csrf

CSRF token. Thymeleaf adds it to the app’s forms as a hidden field, so copy it from the game page.

player-go-first

true if the player moves first and plays X. Defaults to false: the computer moves first and takes the center tile.

Example Request

$ curl 'http://localhost:8080/tictactoe/new-game' -i -X POST \
    --cookie 'JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A' \
    -d 'player-go-first=true&_csrf=q4XkCZs1J7mWb0tR9yHnLgE2vPaF8uDi'
POST /tictactoe/new-game HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:8080
Content-Length: 59
Cookie: JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A

player-go-first=true&_csrf=q4XkCZs1J7mWb0tR9yHnLgE2vPaF8uDi

Example Response

Name Description

Location

The game page, which renders the new game.

HTTP/1.1 302 Found
Content-Language: en
Location: /tictactoe/

Mark Tile

POST /mark-tile marks a tile for the player, then immediately makes a random move for the computer. Nothing changes if the game is already over or the tile ID is malformed. The response is still a redirect to the game page.

Form Parameters

Parameter Description

_csrf

CSRF token. Thymeleaf adds it to the app’s forms as a hidden field, so copy it from the game page.

tile-id

Tile to mark, as {row}-{col} with 0-based indexes, e.g. 0-0 is top left and 1-1 is the center.

Example Request

$ curl 'http://localhost:8080/tictactoe/mark-tile' -i -X POST \
    --cookie 'JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A' \
    -d 'tile-id=1-1&_csrf=q4XkCZs1J7mWb0tR9yHnLgE2vPaF8uDi'
POST /tictactoe/mark-tile HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:8080
Content-Length: 50
Cookie: JSESSIONID=B6C1E2F0A9D84C7E5F3A2B1C0D9E8F7A

tile-id=1-1&_csrf=q4XkCZs1J7mWb0tR9yHnLgE2vPaF8uDi

Example Response

Name Description

Location

The game page, which renders the updated board.

HTTP/1.1 302 Found
Content-Language: en
Location: /tictactoe/

User

These endpoints are provided by Spring Security.

Log In

  • GET /login renders the login page.

  • POST /login authenticates the user with form parameters username, password, _csrf and the optional remember-me (on keeps the user logged in across browser sessions). On success it redirects to the page that was originally requested, or to the game page. On failure it redirects to /login?error.

Log Out

POST /logout (with _csrf) ends the session, deletes the JSESSIONID cookie and redirects to /login?logout.