# PUT

Handle a PUT request with @PutMapping, taking the category's id from the URL and its new values from the body, update it with a JdbcTemplate and return the updated category with a 200 status.

Journeyman · Spring Boot · REST API · Updated November 14, 2026

Updating a record should be done through a PUT request. When a client PUTs a record - they must include the information that will be updated in the body. Additionally, the record id should be included in the URL

![A PUT request names the category in the address (/categories/10) and carries its new values in the body](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/09/ab1cb8ef075f4e0b98fd530712371dbb.png "A PUT request names the category in the address (/categories/10) and carries its new values in the body")

The server will update the data, and then, just like a POST, it should respond with the full resource as it is now saved.

![The server answers 200 (OK), and the body is the updated category: categoryId 10, with its new description](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/01eae07e1bfa4bcea82d38d676f95522.webp)

## PUT Request - Client

In Insomnia, this is your `Update Snacks` request from *Insomnia: PUT*, with the whole category as JSON on its **Body** tab:

```text
PUT {{ _.base_url }}/api/categories/10
```

```json
{
  "categoryName": "Snacks",
  "description": "Chips, nuts, pretzels and popcorn"
}
```

## PUT Request - Server

The method responsible for handling the request must be marked with a `@PutMapping` annotation to handle the request. The id of the resource must be marked as a `@PathVariable`. The body of the request is passed as a method parameter using the `@RequestBody` annotation. The method returns the updated category, and a successful request returns the default status code of 200 (OK), so no `@ResponseStatus` annotation is needed.

```java
@PutMapping(path="/api/categories/{id}")
public Category updateCategory(@PathVariable int id,
                               @RequestBody Category category)
{
    // the update method of the DAO returns
    // the category as it is now saved in the database
    return categoryDao.update(id, category);
}
```

### DAO Logic

The DAO updates the record, using the id from the URL, not one from the body: the address is the one that says which category to change. Then, just as the `insert` method did, it reads the row back, so the caller gets every column as it is now saved.

```java
public Category update(int id, Category category)
{
    String sql = """
            UPDATE categories
            SET category_name = ?
                , description = ?
            WHERE category_id = ?
            """;

    jdbcTemplate.update(sql,
            category.getCategoryName(),
            category.getDescription(),
            id);

    // read the updated row back, so the caller gets every column
    return getCategoryById(id);
}
```

Why send back a category the client just sent? Because what the database saved can differ from what was sent, for example a column the database fills in itself. Answering with the saved record means the client shows exactly what is stored, without a second GET request.

Older APIs often answered a PUT with 204 (no content) and an empty body. It still works, but returning 200 with the updated record is the common choice today.

And if there is no category with that id? The API should answer 404 (not found). The lesson on returning a `ResponseEntity<T>`, later in this sprint, shows how to choose that status code.

## PUT Response - Client

Add the method to your controller and the `update` method to your DAO, run the app again, and send `Update Snacks`. The `404 Not Found` from *Insomnia: PUT* is now `200 OK`, with the category as it's now saved. Send `Update Snacks, name only` too: its answer's `description` is `null`, because a PUT replaces the whole item.

![Insomnia shows 200 OK and the updated category, categoryId 10, with its new description](https://www.ask-codey.com/lessons/TODO)

## Cheat sheets

- [PUT and DELETE](https://www.ask-codey.com/cheat-sheets/restful-web-apis-put-insomnia-cheat-sheet)

Source: https://www.ask-codey.com/lessons/restful-web-apis-put
