我正在查看Spring,我印象非常深刻。发展很快。我有一个应用程序,但是,所有的例子都有控制器包中的主要方法。例如com.demo我想把控制器移到它自己的包里。例如com.demo.controller。然后将主方法留在基本包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);
}
}我的控制器如下
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不确定我是缺少了注释,还是需要拆分一些方便的注释。
发布于 2016-06-25 03:42:47
这不是SpringApplication.run(StudentController.class, args);,而是SpringApplication.run(Application.class, args);。
请小心地遵循指南。
发布于 2016-06-25 03:46:46
在主类中,它不是调用控制器类,而是调用应用程序类,而是解决了这个问题。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}@AutoConfiguration不是必要的,因为@SpringBootApplication内部包含
发布于 2016-06-25 03:46:18
您的应用程序类必须如下所示:
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 {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}https://stackoverflow.com/questions/38024482
复制相似问题