我创建了一个@RepositoryRestResource
@RepositoryRestResource(collectionResourceRel = "tracks", path = "tracks")
public interface TrackRepository extends PagingAndSortingRepository<TrackEntity, Long> {
}
除了一些其他@RestController
:
@RestController
@RequestMapping("/api")
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping(value = "/users", method = RequestMethod.POST)
public @ResponseBody
User postUser(@Validated @RequestBody Credentials credentials) {
return this.userService.postUser(credentials); // Register user
}
}
在我的应用程序中,我正在设置的属性
spring.data.rest.base-path=/api
而这是@SpringBootApplication
入口点:
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"io.app.spring.repository"})
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
@EntityScan(basePackages = "io.app.hibernate.model")
@EnableTransactionManagement
public class Application {
private final static Logger LOGGER = LogManager.getLogger(Application.class);
@PostConstruct
void started() {
TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
}
@Autowired
public Application(Environment environment) {
LOGGER.info("");
LOGGER.info("Active profiles:");
for (String profile : environment.getActiveProfiles()) {
LOGGER.info(" " + profile);
}
LOGGER.info("");
}
public static void main(String[] args) {
LOGGER.debug("Running application ..");
SpringApplication.run(Application.class, args);
}
}
尽管如此,我仍然没有看到 https://localhost:8443/v3/api-docs 下TrackRepository
的端点。只有那些来自UserController
:
..
"/api/users": {
"post": {
"operationId": "postUser",
"requestBody": {
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Credentials"
}
}
}
},
"responses": {
"200": {
"description": "default response",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
}
}
}
}
}
..
我正在使用弹簧启动2.2.2.RELEASE
。
这是我正在使用的整个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>
<groupId>audio-platform</groupId>
<artifactId>server</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.boot.version>2.2.2.RELEASE</spring.boot.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- Spring Framework Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>1.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<!-- Spring Docs (Swagger) -->
<!-- TODO After version upgrades check https://github.com/springdoc/springdoc-openapi/issues/133 -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-core</artifactId>
<version>1.1.49</version>
<exclusions>
<exclusion>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.1.49</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.44</version>
</dependency>
<!-- Sentry -->
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring</artifactId>
<version>1.7.23</version>
</dependency>
<!-- PostgreSQL Driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.4</version>
</dependency>
<!-- Flyway -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb.flyway-test-extensions</groupId>
<artifactId>flyway-spring-test</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Java version -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<!-- Dependency Management -->
<dependencyManagement>
<dependencies>
<!-- Import dependency management from Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
我已经尝试按照此处的建议添加springfox
依赖项
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
但它仍然不起作用。
任何想法可能是什么原因?
请尝试像这样将@Import(SpringDataRestConfiguration.class)
添加到您的应用程序配置中。
compile('io.springfox:springfox-swagger2:2.7.0')
compile('io.springfox:springfox-data-rest:2.7.0')
compile('io.springfox:springfox-swagger-ui:2.7.0')
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"io.app.spring.repository"})
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
@EntityScan(basePackages = "io.app.hibernate.model")
@EnableTransactionManagement
@Import(SpringDataRestConfiguration.class)//<-- Add thisconfiguration
public class Application {
示例:https://reflectoring.io/documenting-spring-data-rest-api-with-springfox/