为什么我的REST端点没有公开?尽管应用程序成功运行.有人能指出我的错误吗



我被卡住了,因为我无法访问存储库中的API。我的2个实体产品.java

package com.udemy.springboot.demo.Entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.*;
import java.util.Date;
@Entity
@Data
@DynamicUpdate
@NoArgsConstructor
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long Id;
@ManyToOne
@JoinColumn(name = "category_Id",nullable = false)
private ProductsCategory category;

@Column(name = "sku")
private String sku;;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "unit_price")
private int unitPrice;
@Column(name = "image_url")
private String imageUrl;
@Column(name = "active")
private boolean active;
@Column(name = "units_in_stock")
private int unitInStock;

@Column(name = "date_created")
@CreationTimestamp
private Date createDate;
@Column(name = "last_updated")
@UpdateTimestamp
private Date lastDate;

}

和我的另一个实体产品类别.java

package com.udemy.springboot.demo.Entity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name="productcategory")
@Getter()
@Setter()
@NoArgsConstructor
public class ProductsCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id")
private long productId;
@Column(name = "category_name")
private int categoryName;
@OneToMany(cascade = CascadeType.ALL,mappedBy = "category")
private Set<Product> allproducts;
}

我的两个存储库是ProductRepository和ProductCategoryRepository

package com.udemy.springboot.demo.Dao;
import com.udemy.springboot.demo.Entity.ProductsCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;
@RepositoryRestResource(collectionResourceRel = "productcategory",path = "productcategory")
public interface ProductCategoryRepository extends JpaRepository<ProductsCategory,Long> {
}

package com.udemy.springboot.demo.Dao;
import com.udemy.springboot.demo.Entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductsRepository extends JpaRepository<Product,Long> {
}

主类

package com.udemy.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

我的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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Udemy.SpringBoot</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Ecommerce 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>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

我的表有条目。当我运行应用程序时,控制台看起来像这个


.   ____          _            __ _ _
/\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::                (v2.4.5)
2021-04-26 09:47:10.471  INFO 14628 --- [           main] c.udemy.springboot.demo.DemoApplication  : Starting DemoApplication using Java 11.0.6 on DESKTOP-MTUECVM with PID 14628 (D:IdeaProjectsEcom LEVISbackendtargetclasses started by uddeshya1 in D:IdeaProjectsEcom LEVISbackend)
2021-04-26 09:47:10.474  INFO 14628 --- [           main] c.udemy.springboot.demo.DemoApplication  : No active profile set, falling back to default profiles: default
2021-04-26 09:47:11.577  INFO 14628 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-04-26 09:47:11.619  INFO 14628 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 36 ms. Found 2 JPA repository interfaces.
2021-04-26 09:47:11.969  INFO 14628 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-04-26 09:47:11.975  INFO 14628 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-04-26 09:47:11.976  INFO 14628 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-04-26 09:47:12.351  INFO 14628 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-04-26 09:47:12.351  INFO 14628 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1842 ms
2021-04-26 09:47:12.583  INFO 14628 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-04-26 09:47:12.614  INFO 14628 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.30.Final
2021-04-26 09:47:12.691  INFO 14628 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-04-26 09:47:12.760  INFO 14628 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-04-26 09:47:13.055  INFO 14628 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-04-26 09:47:13.071  INFO 14628 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2021-04-26 09:47:14.158  INFO 14628 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-04-26 09:47:14.165  INFO 14628 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-04-26 09:47:14.209  WARN 14628 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-04-26 09:47:14.334  INFO 14628 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-26 09:47:15.987  INFO 14628 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-04-26 09:47:16.075  INFO 14628 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-04-26 09:47:16.087  INFO 14628 --- [           main] c.udemy.springboot.demo.DemoApplication  : Started DemoApplication in 6.008 seconds (JVM running for 7.699)
2021-04-26 09:47:16.742  INFO 14628 --- [(3)-192.168.0.4] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-26 09:47:16.743  INFO 14628 --- [(3)-192.168.0.4] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-04-26 09:47:16.744  INFO 14628 --- [(3)-192.168.0.4] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

请帮帮我,我哪里做错了。

这是因为您没有用@RestController注释的ProductController.java类。此文件是所有产品API请求的入口点,不是您的ProductRepository

你可以这样做:

// Controller File
@RestController
@RequestMapping(path = "/products", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE, TEXT_PLAIN_VALUE})
@AllArgsConstructor
public class ProductController {
private final ProductService productService;
@GetMapping()
public ResponseEntity<List<ProductResponseModel>> findAll() {
List<ProductResponseModel> response = productService.findAll());
return new ResponseEntity<>(response, OK);
}
}

// Service File
@Service
public class ProductService {
private final ProductRepository productRepository;
public List<ProductResponseModel> findAll() {
return productRepository.findAll();
}
}

相关内容

最新更新