首页 > 解决方案 > 创建名为“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>

我将不胜感激任何帮助,在此先感谢

标签: javaspringspring-bootmavenspring-data-jpa

解决方案


如果您检查错误,您可以看到它来自那里:

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属性


推荐阅读