I'm brand new to Spring Boot, and I've been walking through this tutorial (https://www.youtube.com/watch?v=vtPkZShrvXQ), but I am stuck at this point. Any pointers would be greatly appreciated.
I'm able to make a POST request and see the request go through with a 200 Status in Postman, but when I make a GET request to retrieve JSON data, I see a 200 Status, yet there is no response in Postman console, only a "ø" - anyone know what I could be doing wrong?
Here is my Controller:
package com.example.demo.api;
import com.example.demo.model.Person;
import com.example.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
@RequestMapping("/api/v1/person")
@RestController
public class PersonController {
//reference to the service
private final PersonService personService;
@Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}
//POST
@PostMapping
public void addPerson(@RequestBody Person person) {
personService.addPerson(person);
}
//GET
@GetMapping
public List<Person> getAllPeople() {
return personService.getAllPeople();
}
}
Here is the service file:
package com.example.demo.service;
import com.example.demo.dao.PersonDao;
import com.example.demo.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Service
public class PersonService {
private final PersonDao personDao;
@Autowired
public PersonService(@Qualifier("fakeDao") PersonDao personDao) {
this.personDao = personDao;
}
public int addPerson(Person person) {
return personDao.insertPerson(person);
}
public List<Person> getAllPeople() {
return personDao.selectAllPeople();
}
public Optional<Person> getPersonById(UUID id){
return personDao.selectPersonById(id);
}
}
And here is the interface:
package com.example.demo.dao;
import com.example.demo.model.Person;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
//insert person into database
public interface PersonDao {
//this inserts a person with an id
int insertPerson(UUID id, Person person);
//this inserts a person that does not have an id. An idea is generated:
default int insertPerson(Person person) {
UUID id = UUID.randomUUID();
return insertPerson(id, person);
}
List<Person> selectAllPeople();
Optional<Person> selectPersonById(UUID id);
int deletePersonById(UUID id);
int updatePersonById(UUID id, Person person);
}