springboot api不起作用。

时间:2018-08-03 05:02:53

标签: java web-services spring-boot swagger-ui

它是一个简单的代码,但是我缺少一些我没有得到的东西,该应用程序正在运行,但API无法正常工作。在运行代码时,它不会记录公开的api详细信息。在调试时使用RequestMappingHandlerMapping不能调试映射的api。你能帮我吗

Controller.java:

package com.mindtree.controllers;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

@RequestMapping("/find")
    @ResponseBody
    public ResponseEntity<String> findEmployee(){
        String emp;
        emp="id is: ";
        return new ResponseEntity<String>(emp,HttpStatus.OK);
    }
}

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>com.mindtree</groupId>
    <artifactId>mydemo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mydemo1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Mydemo1Application.java:

package com.mindtree.mydemo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
public class Mydemo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Mydemo1Application.class, args);
    }
}


@Configuration
@EnableSwagger2
class SwaggeConfiguration {

}

Application.properties:

server.port=8085

1 个答案:

答案 0 :(得分:1)

它是由组件扫描的基本程序包引起的。 should refer best practice of spring boot package layout 它说,

  

我们通常建议您将主应用程序类放在其他类之上的根包中。 @SpringBootApplication批注通常放在您的主类上,它隐式定义某些项目的基本“搜索包”。

这意味着如果要将com.mindtree.mydemo1.controllers定义为基本软件包,应将控制器软件包移至com.mindtree.mydemo1