java - 创建名为“orderController”的 bean 时出错
问题描述
我正在学习Spring Boot
,我想创建一个Rest Controller
,并使用一个sql数据库,但是在我启动项目时出现问题:
错误:( 错误文字很好,我会留下 链接)
和代码:
订单控制器.java
package ***;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@RestController
public class OrderController {
@Autowired
private OrderRepository orderRep;
@GetMapping("/")
public List<Order> index(){
return (List<Order>) orderRep.findAll();
}
@GetMapping("/{id}")
public Order show(@PathVariable int id){
Optional<Order> orderId= orderRep.findById(id);
Order order = orderId.get();
return order;
}
@PostMapping("/search")
public List<Order> search(@RequestBody Map<String, String> body){
String searchItem = body.get("item");
return orderRep.findByTitleContainingOrContentContaining(searchItem);
}
@PostMapping("/")
public Order create(@RequestBody Map<String, String> body){
String item = body.get("item");
int price = Integer.parseInt(body.get("price"));
int quantity = Integer.parseInt(body.get("quantity"));
return orderRep.save(new Order(item, price,quantity));
}
@PutMapping("/{id}")
public Order update(@PathVariable String id, @RequestBody Map<String, String> body){
int orderId = Integer.parseInt(id);
// getting blog
Optional<Order> order = orderRep.findById(orderId);
order.get().setItem(body.get("item"));
order.get().setPrice(Integer.parseInt(body.get("price")));
order.get().setQuantity(Integer.parseInt("quantity"));
return orderRep.save(order.get());
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable int id){
//int orderId = Integer.parseInt(id);
orderRep.deleteById(id);
return true;
}
}
订单.java
package ***;
import javax.persistence.*;
@Entity
@Table(name = "orders")
public class Order {
//Fields
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "item")
private String item;
@Column(name = "price")
private int price;
@Column(name = "quantity")
private int quantity;
//Constructors
public Order() { }
public Order(String item, int price, int quantity) {
this.setItem(item);
this.setPrice(price);
this.setQuantity(quantity);
}
public Order(int id, String item, int price, int quantity) {
this.setId(id);
this.setItem(item);
this.setPrice(price);
this.setQuantity(quantity);
}
//Object to string data
@Override
public String toString() {
return "Order{" +
"id=" + getId() +
", item='" + getItem() + '\'' +
", price='" + getPrice() + '\'' +
", price='" + getQuantity() + '\'' +
'}';
}
//Getters & Setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
OrderRepository.java
package ***;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface OrderRepository extends CrudRepository<Order, Integer> {
// custom query to search to blog post by title or content
List<Order> findByTitleContainingOrContentContaining(String item);
}
应用程序属性
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/testvoy?useUnicode=true&serverTimezone=UTC&useSSL=true&verifyServerCertificate=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
#spring.jpa.show-sql: true
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project</name>
<description>project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.8</version>
<executions>
<execution>
<id>generate-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<backend>html</backend>
<doctype>book</doctype>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-asciidoctor</artifactId>
<version>${spring-restdocs.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我将不胜感激任何帮助,在此先感谢
解决方案
如果您检查错误,您可以看到它来自那里:
List<Order> findByTitleContainingOrContentContaining(String item);
如果您更仔细地阅读错误,您将看到:
没有找到类型 Order! 的属性标题;
这里里面:
原因:org.springframework.data.repository.query.QueryCreationException:无法为公共抽象 java.util.List com.test.project.OrderRepository.findByTitleContainingOrContentContaining(java.lang.String) 创建查询!原因:无法为方法 public abstract java.util.List com.test.project.OrderRepository.findByTitleContainingOrContentContaining(java.lang.String) 创建查询!没有找到类型 Order! 的属性标题;嵌套异常是 java.lang.IllegalArgumentException: 无法为方法 public abstract java.util.List com.test.project.OrderRepository.findByTitleContainingOrContentContaining(java.lang.String) 创建查询!未找到 Order 类型的属性标题!
事实上,如果你检查你的模型,没有title
属性
推荐阅读
- c - 从C中的二进制文件中删除结构
- amazon-web-services - 使用 Boto3 通过 AWS SNS 通知发送图像
- sql - 带有特定查询的 Fill 方法中的日历日期字符串转换
- javascript - 使用 .find 方法不会从解析的 JSON 返回对象
- python - Python 和 R 中的 Keras 默认后端
- javascript - 如何更改我的代码以使用户只能喜欢该帖子一次?
- javascript - 使用反应 useState 和 useEffect 的内存泄漏
- python - 为什么 pytest 在使用 python -m test 运行时取消选择所有测试?
- c++ - 为什么 std::set::insert() 使用错误参数调用 operator<()
- postgresql - 如何在postgresql的多行的单列中添加值