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
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
From Postman I fill in the body as below to perform the Get
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;
}
}