我最近一直在一个Spring Boot项目上工作,该项目以JSON格式从MySQL数据库更新和获取数据,但是当我运行我的应用程序时,我看到了一个错误页面:
[我的浏览器上的错误截图] [1] [1]:https://i.stack.imgur.com/CkYmr.png
我的实体类为:
package com.project.project.entities;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@Entity
@Table(name = "products")
// why serializable ?? every entity in JPA is automatically-serializable, connection between different networks
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // assign a unique value to your identity field automatically
private Long id;
private String designation;
private int price;
private int quantity;
private int category_id;
// the owning side of the relationship, side of the foreign key
@ManyToOne(fetch = FetchType.LAZY )// many products to one category
@JoinColumn(name = "category_id" , insertable = false , updatable = false)
// means that the product table will have a fk_column named...
private Category category;
// categoryId foreign key referencing to the primary key on Category
// Double and Integer in case both variables are unknown -> Category constructor
public Product(Long id, String designation, Integer price, Integer quantity, int categor_id) {
this.id = id;
this.designation = designation;
this.price = price;
this.quantity = quantity;
this.category_id = category_id;
}
我的存储库:
import com.project.project.entities.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends JpaRepository<Product,Long> {
}
我的服务组件:
import com.project.project.dao.ProductRepository;
import com.project.project.entities.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ProductService {
@Autowired // establish injection
private ProductRepository productRepository;
// GET-ALL
public List<Product> getAllProducts() {
List<Product> products = new ArrayList<>();
productRepository.findAll().forEach(products::add);
return products;
}
// GET
public Product getProduct(Long id) {
return productRepository.getOne(id);
}
// POST
public void addProduct(Product product){
productRepository.save(product);
}
// PUT
public void updateProduct(Long id, Product product) {
product.setId(id);
productRepository.save(product);
}
// DELETE
public void deleteProduct(Long id){
productRepository.deleteById(id);
}
}
我的REST控制器是:
import com.project.project.entities.Product;
import com.project.project.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
public class ProductRestController {
@Autowired
ProductService productService;
/* @Autowired // establish injection
public ProductRepository productRepository;*/
// GET-ALL
@GetMapping(value = "/all")
public @ResponseBody List<Product> getAllProducts() {
return productService.getAllProducts();
}
/*@GetMapping(value = "/products/{id}")
@ResponseBody
public List<Product> getProducts(@RequestParam String designation) {
return productRepository.findByDesignation(designation);
}*/
// GET
@GetMapping(value = "/products/{id}")
public Product getProduct(@PathVariable(name = "id") Long id) {
return productService.getProduct(id);
}
// PUT
@PutMapping(value = "/products/{id}")
public void updateProduct(@PathVariable(name = "id") Long id, @RequestBody Product product) {
productService.updateProduct(id, product);
}
// POST
@PostMapping(value = "/products")
public void save(@RequestBody Product product) {
productService.addProduct(product);
}
// DELETE
@DeleteMapping(value = "/products/{id}")
public void delete(@PathVariable(name = "id") Long id) {
productService.deleteProduct(id);
}
}
我的application.properties:
spring.datasource.url = jdbc:mysql://localhost:3306/sport_shop?serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = emisql
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
server.port = 9091
idea.spring.boot.filter.autoconfig=false
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
还有我的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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
你们认为问题出在哪里?
答案 0 :(得分:0)
只需将@Controller
设置为控制器上的注释。
//imports
@Controller
public class ProductRestController {
@Autowired
ProductService productService;
//GetMapping etc
}
您也可以使用@RestController
注释。在此处详细了解差异:
Difference between spring @Controller and @RestController annotation