gpt4 book ai didi

java - springboot api 不工作。

转载 作者:行者123 更新时间:2023-12-02 10:56:47 24 4
gpt4 key购买 nike

这是一个简单的代码,但我缺少一些我没有得到的东西,应用程序正在运行,但 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 {

}

应用程序属性:

server.port=8085

最佳答案

是组件扫描基础包造成的。 should refer best practice of spring boot package layout它说,

We generally recommend that you locate your main application class in a root package above other classes. The @SpringBootApplication annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.

这意味着如果您想将 com.mindtree.mydemo1 定义为基础包,您应该将 Controller 包移至 com.mindtree.mydemo1.controllers

>

关于java - springboot api 不工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51665622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com