Each mapping answers one method on one path. When a request comes for a path that exists, but with a method no mapping handles, Spring answers 405 Method Not Allowed. With it, Spring sends an Allow header: the list of methods the path does accept. A path with no mapping at all still gets 404 Not Found.
For example, / has only a GET mapping, so DELETE / gets 405 with Allow: GET. You never write code for a 405: to allow a method on a path, you add a mapping for it.
That's what you'll do for /about. Its answer used to be built inside the method with Map.of, so nothing could change it. You'll keep the shop's details in a field shop that both methods share, and add a PUT mapping that replaces them. The requests and answers your code must give, in this order:
PUT /products {"name": "Cup", "price": 5, "category": "kitchen"}
→ 405 Allow: GET, POST
POST /products/2 {"price": 5}
→ 405 Allow: GET, PATCH, DELETE, PUT
DELETE /about → 405 Allow: GET, PUT
PUT /about {"name": "Corner Store", "currency": "EUR"}
→ 200 {"name": "Corner Store", "currency": "EUR"}
GET /about → 200 {"name": "Corner Store", "currency": "EUR"}
GET /contact → 404
Your task: keep the about details in a HashMap field shop, make about return it, and add PUT /about, whose method updateAbout puts the name and currency from the body into shop and returns the new details. Then /about allows GET and PUT.
Assignment
Follow the Coding Tutorial to store the shop's details in a field and add a PUT mapping for /about.
Hintshop is a field of the controller, outside the methods, so both methods read and change the same map. It must be a new HashMap<>(Map.of(…)): a Map.of map can't change. The 405 answers need no code: Spring sends them, with the Allow header, for every method a path has no mapping for.
Free plan: 30 tutor messages. PRO is unlimited.
