我买了这本新书,试图快速学习 Spring Boot。开始的时候很好,我很容易创建了一个 REST API。但后来我们添加了 CrudRepository,我发现书中描述的代码存在问题。此外,没有代码可供下载,因为作者从 Oreily 的 git repo 中删除了它以修复一些问题......
问题是,如果我尝试按照书中描述的那样构建代码(没有默认构造函数),我会收到一个 Java 错误,抱怨没有默认构造函数。如果我添加一个默认构造函数,它会构建,但 Spring 使用它而不是新的构造函数,这需要传递一个参数。因此,当我实际调用 API 时,例如调用 /coffees 端点时,我得到一个 java.lang.NullPointerException: null
那么 Spring 应该如何知道要使用哪个构造函数,以及它如何为这个参数传递值呢?
这是控制器:
package com.bw.restdemo;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/coffees")
class RestAPIDemoController {
private final CoffeeRepository coffeeRepository;
public RestAPIDemoController(CoffeeRepository coffeeRepository) {
this.coffeeRepository = coffeeRepository;
this.coffeeRepository.saveAll(List.of(
new Coffee("Cafe Cereza"),
new Coffee("Freedom Fuel"),
new Coffee("Cold Brew"),
new Coffee("Sumatra")
));
}
public RestAPIDemoController() {
this.coffeeRepository = null;
};
//@RequestMapping(value = "/coffees", method = RequestMethod.GET)
@GetMapping
Iterable<Coffee> getCoffees() {
return coffeeRepository.findAll();
}
@GetMapping("/{id}")
Optional<Coffee> getCoffeeById(@PathVariable String id) {
return coffeeRepository.findById(id);
}
@PostMapping
Coffee postCoffee(@RequestBody Coffee coffee) {
return coffeeRepository.save(coffee);
}
@PutMapping("/{id}")
ResponseEntity<Coffee> putCoffee(@PathVariable String id, @RequestBody Coffee coffee) {
return (!coffeeRepository.existsById(id))
? new ResponseEntity<>(coffeeRepository.save(coffee), HttpStatus.CREATED)
: new ResponseEntity<>(coffeeRepository.save(coffee), HttpStatus.OK);
}
@DeleteMapping("/{id}")
void deleteCoffee(@PathVariable String id) {
coffeeRepository.deleteById(id);
}
}
这是我定义接口的地方:
package com.bw.restdemo;
import org.springframework.data.repository.CrudRepository;
interface CoffeeRepository extends CrudRepository<Coffee, String> {
}
这是主要课程- 为塞在底部的课程道歉。
package com.bw.restdemo;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RestDemoApplication.class, args);
}
}
@Entity
class Coffee {
@Id
private String id;
private String name;
public Coffee(String id, String name) {
this.id = id;
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public Coffee(String name) {
this(UUID.randomUUID().toString(), name);
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}