3 回答

TA貢獻1794條經驗 獲得超7個贊
我認為問題在于您使用注釋的方式,嘗試將注入更改為構造函數,例如:
@RestController
public class AppUserController {
private BookRepository bookRepository;
@Autowired
public AppUserController (
BookRepository bookRepository){
this.bookRepository= bookRepository;
}
@GetMapping("/booksave")
public String helloworld() {
bookRepository.save(new Book(3L, "author"));
return "book saved";
}
}
理解它的來源:Spring @Autowire on Properties vs Constructor

TA貢獻1856條經驗 獲得超11個贊
如何使用 Spring 數據休息:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
你不需要編寫控制器代碼
import org.springframework.cloud.gcp.data.datastore.repository.DatastoreRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "xxxxxs", path = "xxxx")
public interface XXXXXRepository extends DatastoreRepository<XXXXX, String>
大搖大擺的配置?。。?/p>
@Configuration
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class SwaggerConfig {
添加回答
舉報