'Spring Boot Post and Get Request not working
I'm new to Springboot and making post/get requests, and I've been following a youtube tutorial to better understand it with postman. However, I've run into a problem while tinkering with the code, I'm trying to allow a post request for 2 entries
{
"name": "Hat",
"price": "$1"
}
however, whenever I try to send a get request it returns
{
"price": "null"
"name": "Hat"
}
I believe the problem is that the price is not being posted, thus the .orElse(null) is executing. I'm not sure why this is the case, and I would greatly appreciate the help. If any further information is needed let me know and I'll post it.
package com.example.demo.dao;
import com.example.demo.model.Person;
import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Repository("fakeDao") //class serves as a repository
public class FakePersonDataAcessService implements PersonDao{
private static List<Person> DB = new ArrayList<>();
@Override
public int insertPerson(Person/*BigDecimal*/ price, Person person) {
DB.add(new Person(price.getPrice(), person.getName()));
return 1;
}
@Override
public List<Person> selectAllPeople() {
return DB;
}
@Override
public Optional<Person> selectPersonByPrice(Person/*BigDecimal*/ price) {
return DB.stream()
.filter(person -> person.getPrice().equals(price)) //filters through the people and checks our DB to see if that person with that price exists
.findFirst();
}
@Override
public int deletePersonByPrice(Person/*BigDecimal*/ price) {
Optional<Person> personMaybe = selectPersonByPrice(price);
if (personMaybe.isEmpty()){
return 0;
}
DB.remove(personMaybe.get());
return 1;
}
@Override
public int updatePersonByPrice(Person/*BigDecimal*/ price, Person update) {
return selectPersonByPrice(price) //select the person
.map(person -> { //map the person
int indexOfPersonToUpdate = DB.indexOf(person);
if (indexOfPersonToUpdate >= 0){ //if the index of that person is >=0 we know that we have found that person
DB.set(indexOfPersonToUpdate, new Person(price.getPrice(), update.getName())); //set contents of that person to the new person that we just recieved from thc client
return 1; //return 1 if everything is fine
}
return 0; //otherwise we return 0 or if selectpersonbyprice is not present we dont do anyhthing and just return 0
})
.orElse(0);
}
}
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.math.BigDecimal;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
@RequestMapping("api/v1/person")
@RestController
public class PersonController {
private final PersonService personService;
@Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}
@PostMapping
public void addPerson(Person price,/*@Valid @NotNull Stringprice,*/@Valid @NotNull @RequestBody Person person){
personService.addPerson(price,/*price,*/ person);
}
@GetMapping
public List<Person> getAllPeople(){
return personService.getAllPeople();
}
@GetMapping(path = "{price}")
public Person getPersonByPrice(@PathVariable("price") Person/*BigDecimal*/ price){
return personService.getPersonByPrice(price) //after sending a get request with that price we will either return the person, OR if they don't exisist we return null
.orElse(null);
}
@DeleteMapping(path = "{price}")
public void deletePersonByPrice(@PathVariable("price") Person/*BigDecimal*/ price){
personService.deletePerson(price);
}
@PutMapping(path = "{price}")
public void updatePerson(@PathVariable("price") Person/*BigDecimal*/ price, @Valid @NotNull @RequestBody Person personToUpdate){
personService.updatePerson(price, personToUpdate);
}
}
package com.example.demo.dao;
import com.example.demo.model.Person;
import javax.swing.text.html.Option;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
//actual interface where we can define our operations allowed/contract for anyone who wants to implement this interface; and using dependecy injection we can change db easily
public interface PersonDao {
int insertPerson(Person price, Person person); //allows us to insert a person with a given price
/*
default int insertPerson(Person price, Person person){
String price = new String("$"); //without an price, its default is 0
return insertPerson(price, person);
}
*/
List<Person> selectAllPeople();
Optional<Person> selectPersonByPrice(Person/*BigDecimal*/ price);
int deletePersonByPrice(Person/*BigDecimal*/ price);
int updatePersonByPrice(Person/*BigDecimal*/ price, Person person);
}
package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.UUID;
public class Person {
private final String price;
@NotBlank //name can't be blank
private final String name;
public Person(@JsonProperty("price") String /*BigDecimal*/ price,
@JsonProperty("name") String name) {
this.price = price;
this.name = name;
}
public String/*BigDecimal*/ getPrice() {
return price;
}
public String getName() {
return name;
}
}
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 org.springframework.web.bind.annotation.GetMapping;
import java.math.BigDecimal;
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 price,/*String price,*/ Person person){
return personDao.insertPerson(price,/*price,*/ person);
}
@GetMapping
public List<Person> getAllPeople(){
return personDao.selectAllPeople();
}
public Optional<Person> getPersonByPrice(Person/*BigDecimal*/ price){
return personDao.selectPersonByPrice(price);
}
public int deletePerson(Person/*BigDecimal*/ price){
return personDao.deletePersonByPrice(price);
}
public int updatePerson(Person/*BigDecimal*/ price, Person newPerson){
return personDao.updatePersonByPrice(price, newPerson);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
