gpt4 book ai didi

java - 如何将设置 Spring Boot 的主要方法移动到基础包中而不破坏它

转载 作者:行者123 更新时间:2023-12-02 03:31:34 26 4
gpt4 key购买 nike

我正在查看 Spring Boot,给我留下了深刻的印象。发展非常快。我有一个应用程序,但是,所有示例都有 Controller 包中的 main 方法。 [例如。 com.demo] 我想将 Controller 移动到它自己的包中。 [例如。 com.demo.controller]。然后将main方法留在基础包[com.demo]中。当我这样做的时候,世界就被打破了。似乎某些注释可能需要分离出来,或者便捷方法可能需要分解。我已将我的 Application.java 移至基础包中,正如我在上面所述,并移动了我认为正确的注释,但似乎我遗漏了一些东西。这是我的代码。

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.demo.controllers.StudentController;

@SpringBootApplication
@EnableAutoConfiguration
public class Application {

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

我的 Controller 如下

package com.demo.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.demo.models.Student;

@Controller
@RequestMapping("/student")
public class StudentController {

@RequestMapping(method = RequestMethod.GET)
public List<Student> getAll() {
return new ArrayList<Student>();
}

@RequestMapping(method = RequestMethod.POST)
public Student create(@RequestBody Student Student) {
return null;
}

@RequestMapping(method = RequestMethod.DELETE, value = "{id}")
public void delete(@PathVariable String id) {

}

@RequestMapping(method = RequestMethod.PUT, value = "{id}")
public Student update(@PathVariable String id, @RequestBody Student Student) {
return null;
}
}

我得到的堆栈跟踪是这样的。

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at com.demo.Application.main(Application.java:14) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
... 8 common frames omitted

不确定我是否缺少注释或需要拆分一些方便注释。

最佳答案

它不是 SpringApplication.run(StudentController.class, args); 而是 SpringApplication.run(Application.class, args);

仔细遵循指南。

关于java - 如何将设置 Spring Boot 的主要方法移动到基础包中而不破坏它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38024482/

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