Spring boot RestControllers 不适用于包含 @Entities 的库

问题描述 投票:0回答:1

我有一个在 java 17 上运行的 spring boot (3.2.1) 应用程序。我还有包含

@Entity
注释的库。我使用 spring boot 默认 Hibernate 连接到数据库。我可以连接到数据库,但
@RestController
中定义的端点不起作用。我之前从库初始化数据库(传递字符串值),并且使用 EclipseLink 而不是 Hibernate。

我必须重新使用

@EntityScan
从库中获取实体。以前有 persistence.xml。由于某种原因,我能够连接到 Spring Boot 内部的数据库,但
@RequestMapping
不起作用。我将连接作为
EntityManagerFactory
传递到我的图书馆。

我没有使用存储库,我不知道这是否会对功能产生任何影响。

我正在讨论数据库连接问题here

我的控制器包含映射,如下所示:

@RestController
@RequestMapping("${com.example.apiPrefix}/data")
public class MyController extends GodController {


  @Autowired
  private KafkaTemplate<Integer, MyWrapper> kafkaTemplate;


  
  @GetMapping(value = "/id", produces = {"application/json"})
  @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Description...")
  public ResponseEntity<MyWrapper> getId() throws DatatypeConfigurationException {

    ResponseEntity<MyWrapper> res = null;

    .
    .
    .
    .
    return res;
  }

GodController
看起来像这样:

@RequestMapping("${com.example.apiPrefix}")
@OpenAPIDefinition(
        info = @Info(
                title = "Hello World",
                version = "1.0",
                description = "Description"
        )
)
public class GodController {

  @Autowired
  Library libr;

  
  public SctrzController() {
  }
  
}
java spring spring-boot hibernate spring-restcontroller
1个回答
0
投票

所以我解决了这个问题。在 Spring Boot 3 中,应该有端点的 http 地址,末尾带有反斜杠。

我使用的是 localhost:8080/prefix/data

它适用于 localhost:8080/prefix/data/

愚蠢的错误,希望有一天它能帮助别人。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.