1

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);
}
Jon
  • 622
  • 8
  • 29
  • By the way, if you're using JPA, Spring Data JPA will auto-generate your DAO implementation for you and can provide useful help like allowing you to say `@GetMapping("/{id}")... @PathVariable("id") Person person`. Additionally, it's usually a good idea to separate out a DTO class (`PersonDto`) from your database representation so that you can make changes to either as needed without breaking anything. MapStruct is a tool that can help you with conversions to and from DTO representations. – chrylis -cautiouslyoptimistic- May 11 '20 at 21:39
  • Can you please clarify your second paragraph? Which request is returning the empty body? – Savior May 11 '20 at 22:10
  • The GET request is returning the empty body. – Jon May 11 '20 at 23:44

2 Answers2

0

Your code returns 200 (because it returns successfully and doesn't specify any other status code), but it returns an empty body because it's void and doesn't use any other mechanism to return content.

If you like, you can return a DTO object from the method, and whether you do or not you should return ResponseEntity.created(uri) to provide the Location header. MvcUriComponentsBuilder can be helpful in constructing a URI for the new resource.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

Whoops - there was a typo in my Data Access Service File. I don't know how I had the selectAllPeople() method returning "null" instead of the database (DB), but that's what I had:

    @Override
    public List<Person> selectAllPeople() {
        return null;
    }

And the amazing solution, lol:

    @Override
    public List<Person> selectAllPeople() {
        return DB;
    }

Thanks for the feedback and the patience, everyone!

Jon
  • 622
  • 8
  • 29