Приложение Spring Boot POST /api/рецепт/новое должно ответить кодом состояния 200, ответ: 404

#java #spring-boot #api #http-status-code-404

Вопрос:

Вот приложение для весенней загрузки «Рецепты». Мы можем добавить рецепт в конечную точку /api/рецепт/новый, также мы можем получить рецепт по идентификатору в конечной точке /api/рецепт/{id}. В этом случае мы используем HashMap в качестве хранилища.

Это класс рецептов:

 package recipes;  import lombok.Data;  @Data public class Recipe {   private String name;  private String description;  private String[] ingredients;  private String[] directions; }  

Это контроллер RestController

 package recipes;  import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ResponseStatusException; import java.util.HashMap; import java.util.Map;  @RestController @RequestMapping("/api") public class RecipeRestController {   int id = 0;  Maplt;Integer, Recipegt; recipeBook = new HashMaplt;gt;();   @PostMapping(path = "/recipe/new", consumes = "application/json", produces = "application/json")  public Integer adding(@RequestBody Recipe recipe) {  if (!recipeBook.containsValue(recipe)) {  recipeBook.put(  id, recipe);  }  return id;  }   @GetMapping("/recipe/{id}")  public Recipe retrieving(@PathVariable int id) {  Recipe recipe = recipeBook.get(id);  if (recipe == null) {  throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");  }  return recipe;  } }  

Основной класс находится здесь

 package recipes;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  @SpringBootApplication public class RecipesApplication {  public static void main(String[] args) {  SpringApplication.run(RecipesApplication.class, args);  } }  

Тестовый запрос-это JSON

 {  "name": "Fresh Mint Tea",  "description": "Light, aromatic and refreshing beverage, ...",  "ingredients": ["boiled water", "honey", "fresh mint leaves"],  "directions": ["Boil water", "Pour boiling hot water into a mug", "Add fresh mint leaves", "Mix and let the mint leaves seep for 3-5 minutes", "Add honey and mix again"] }  

Хотя это довольно простое приложение, я не могу понять, в чем проблема…

Комментарии:

1. Пожалуйста, добавьте свой тестовый запрос.

2. Просто отредактировал мой вопрос.

3. Вы не включаете способ, которым вы тестируете, как выглядит заголовок запроса? Вы указываете потребителя/производителя в приложении/json … в вашем тестовом запросе? Нормально ли веб-приложение работает с другой конечной точкой…

4. это что, гиперскилловый проект рецептов?

5. Да. Это. Может быть, вы знаете, в чем моя проблема)