【发布时间】:2022-01-26 08:29:59
【问题描述】:
我是 Springboot 的新手并提出 post/get 请求,我一直在关注 youtube 教程,以便更好地理解 postman。但是,我在修改代码时遇到了问题,我正在尝试允许发布 2 个条目的请求
{
"name": "Hat",
"price": "$1"
}
但是,每当我尝试发送 get 请求时,它都会返回
{
"price": "null"
"name": "Hat"
}
我认为问题在于没有发布价格,因此 .orElse(null) 正在执行。我不确定为什么会这样,我将非常感谢您的帮助。如果需要任何进一步的信息,请告诉我,我会发布。
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);
}
}
【问题讨论】:
-
请只发布与问题相关的代码。
-
如果可能的话,你能分享一下 git 链接吗?
-
抱歉,这是 gitlab 教程链接,包含所有基本代码:gitlab.com/geektechniquestudios/learning.30
-
在本教程中,它旨在生成一个随机 UUID,但是,我想这样做,以便我可以发送一个自定义字符串的发布请求。就像我给出的例子一样。
-
哪个映射给出了不正确的答案? getAllPeople 还是 getPersonById?
标签: java spring spring-boot postman