0

I'm trying to get a food object by its foodId but I'm getting 400 error code saying Required String parameter 'foodId' is not present.

Here is my controller class:

    package com.quickmeal.controller;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;


    @RestController
    @RequestMapping("/api/foods")
    public class FoodController {

    @Autowired
    private FoodService foodService;


    @RequestMapping(method = RequestMethod.POST)
    public Food createFood(@RequestBody Food food) {
        return foodService.createFood(food);
    }


    @RequestMapping(method = RequestMethod.GET)
    public List<Food> listFood() {
        return foodService.listFood();
    }

    @RequestMapping(value = "/{foodId}",method = RequestMethod.GET)
    public Object getFoodById(@RequestParam String foodId) {
        System.out.println("Testing..."+foodId);
        return foodService.getFoodById(foodId);

    }


    }

My Service Class:

package com.quickmeal.service.impl;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quickmeal.model.Food;
import com.quickmeal.repository.FoodRepository;
import com.quickmeal.service.FoodService;

@Service
public class FoodServiceImpl implements FoodService{

    @Autowired
    private FoodRepository foodRepository;

    @Override
    public Food createFood(Food food) {

        return foodRepository.save(food);

    }


    @Override
    public List<Food> listFood() {

        return foodRepository.findAll();

    }


    @Override
    public Object getFoodById(String foodId) {

        return foodRepository.findById(foodId);
    }



}

My Model :

package com.quickmeal.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Food {
    @Id
    private String foodId;

    private String foodName;
    private double foodPrice;
    public String getFoodId() {
        return foodId;
    }
    public void setFoodId(String foodId) {
        this.foodId = foodId;
    }
    public String getFoodName() {
        return foodName;
    }
    public void setFoodName(String foodName) {
        this.foodName = foodName;
    }
    public double getFoodPrice() {
        return foodPrice;
    }
    public void setFoodPrice(double foodPrice) {
        this.foodPrice = foodPrice;
    }


}

This is my URL

localhost:8080/api/foods/5b08f8cee731e32c60e95aa8

5b08f8cee731e32c60e95aa8 is the id given by mongoDB

Please Help. Thank You.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317

1 Answers1

1

foodId in this path "/{foodId}" is a @PathVariable not @RequestParam

The correct handle method should be:

@RequestMapping(value = "/{foodId}",method = RequestMethod.GET)
public Object getFoodById(@PathVariable String foodId) {
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51