1

I have a problem using the GET call. I have a small project that through a get call returns me a message with the various specific data

@RestController
@RequestMapping("/resources/")
public class CarController {
    @Autowired
    CarService carService;
    @Autowired
    ObjectMapper objectMapper;
    @GetMapping(
    value = "/cars",
    consumes = {MediaType.APPLICATION_JSON_VALUE},
    produces = {MediaType.APPLICATION_JSON_VALUE})
    public CarsResponse getCars(@RequestBody CarsRequest request) throws IOException {
        //some code
    }

This is the Request that I send

public class CarsRequest implements Serializable {
    private String name;
    private String plate ;
    private  String price;
}

When I go to Postman to run my Get everything works fine enter image description here

Returning me the correct message:

{
   "name":"BMW",
   "plate":"TGR3456T",
   "price":"1500",
   "brand":"",
   "kilometers":"500000",
   "revisiondate":"2020-09-03",
   "owner":"Silverhand"
}

I noticed that trying to Get by inserting one of these "null" fields the call does not find me any related specific message even if in my Database that value is set to null, below is the example enter image description here

From Postman I fill in the body as below to perform the Get enter image description here

It does not return any messages back, failing to match the file in the database. But in mySql you can easily make queries by inserting one of the fields used for the null search (if I'm not mistaken) because instead in this case I can't do it? For the sake of accuracy, I enter my service

@Service
public class CarService {
    @Autowired
    CarRepository carRepository;
    @Autowired
    ObjectMapper objectMapper;

    public List<Car> getByPlate(String name, String plate, String price) throws JsonProcessingException {
        List<Car> car = new ArrayList<>();
        for (Cache.Entry<String, Car> entry: carRepository.findAllByNameAndPlateAndPrice(name, plate,price)){
            car.add(new Car(entry.getKey(), entry.getValue()));
            System.out.println("Entry: " + objectMapper.writeValueAsString(entry));
        }
        return cars;
    }
}
Numero 21
  • 144
  • 9
  • 1
    Sidenote: GET requests *should* **not** have a body – Lino Mar 26 '21 at 14:27
  • This answer is relevant. Essentially there should be nothing in the body that would change the response. If you need to use a get request with parameters, use a query string: https://stackoverflow.com/questions/978061/http-get-with-request-body – rook218 Mar 26 '21 at 14:30
  • If `price` is null, indicating that you want all prices, then you need to call `carRepository.findAllByNameAndPlate(name, plate)`. – Andreas Mar 26 '21 at 14:31

0 Answers0