首页 > 解决方案 > 加载资源失败:服务器响应状态为 404 () 。招摇配置不起作用

问题描述

Swagger 配置不起作用我将 swagger 与 springboot 一起使用,当我点击此 URL http://localhost:8080/swagger-ui.html 时出现 error404。我是招摇的新手,我不知道如何解决它。我创建单独的配置文件加载资源失败:服务器响应状态为 404 () 这里所有文档详细信息

用户配置.kt

包 com.main.swaggerdemo.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2


@Configuration
@EnableSwagger2
class UserConfig {
    @Bean
    fun postApi(): Docket {
        return Docket(DocumentationType.SWAGGER_2)
            .select()
            .paths(PathSelectors.ant("/api/*"))
            .apis(RequestHandlerSelectors.basePackage("com.main.swaggerdemo"))
            .build()

    }

}

用户控制器.kt

package com.main.swaggerdemo.controller

import com.main.swaggerdemo.entity.User
import com.main.swaggerdemo.model.req.ReqUser
import com.main.swaggerdemo.model.response.RespUser
import com.main.swaggerdemo.repo.UserRepo
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
class UserController {
    @Autowired
    private lateinit var userRepo: UserRepo

    @GetMapping("/signup")
    @ApiOperation(value = "create new user")
    fun signupUser(@ModelAttribute request: ReqUser): ResponseEntity<*> {
        val newUser = User(name = request.name, country = request.country,
            email = request.email, password = request.password)
        userRepo.save(newUser)
        val resUser = RespUser(newUser.id, newUser.name, newUser.country, newUser.email)
        return ResponseEntity(resUser, HttpStatus.OK)
    }

    @GetMapping("/findByEmail/{email}")
    @ApiOperation(value = "find the user")
    fun getUserByEmail(@PathVariable("email") email: String): ResponseEntity<*> {
        val curentUser = userRepo.findByEmail(email)
        if (curentUser != null) {
            val userData = userRepo.findByEmail(email)
            return ResponseEntity(userData, HttpStatus.OK)
        }
        return ResponseEntity("No data found", HttpStatus.NOT_FOUND)
    }
}

构建-gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.4.5"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.4.32"
    kotlin("plugin.spring") version "1.4.32"
    kotlin("plugin.jpa") version "1.4.32"
}

group = "com.main"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("io.springfox:springfox-swagger2:3.0.0")
    implementation("io.springfox:springfox-swagger-ui:3.0.0")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    runtimeOnly("mysql:mysql-connector-java")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

标签: spring-bootkotlinswagger

解决方案


我用招摇的方式为我的服务添加了以下更改。你可以试试同样的。

@Configuration
@EnableSwagger2
class UserConfig {

@Bean
public Docket apiDocumentationV1() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .paths(PathSelectors.ant("/v1/**"))
            .build()
            .apiInfo(createMetaData())
            .useDefaultResponseMessages(false);
}

private ApiInfo createMetaData() {
    return new ApiInfoBuilder()
            .title("Test Service APIs")
            .description("API to maintain test service.")
            .version("1.0.0")
            .build();
}
}

推荐阅读