首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将设置Spring的主要方法移动到基本包中而不破坏它

如何将设置Spring的主要方法移动到基本包中而不破坏它
EN

Stack Overflow用户
提问于 2016-06-25 02:52:08
回答 3查看 1.8K关注 0票数 0

我正在查看Spring,我印象非常深刻。发展很快。我有一个应用程序,但是,所有的例子都有控制器包中的主要方法。例如com.demo我想把控制器移到它自己的包里。例如com.demo.controller。然后将主方法留在基本包com.demo中。当我这样做的时候,它会打破这个世界。似乎有些注释需要分离,或者方便的方法可能需要分解。正如我所说的那样,我已经将我的Application.java移到了一个基本包中,并且移动了我认为正确的注释,但是我似乎遗漏了一些东西。这是我的密码。

代码语言:javascript
复制
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);
    }
}

我的控制器如下

代码语言:javascript
复制
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;
    }
}

我得到的堆栈追踪就是这个。

代码语言:javascript
复制
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

不确定我是缺少了注释,还是需要拆分一些方便的注释。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-25 03:42:47

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

请小心地遵循指南。

票数 2
EN

Stack Overflow用户

发布于 2016-06-25 03:46:46

在主类中,它不是调用控制器类,而是调用应用程序类,而是解决了这个问题。

代码语言:javascript
复制
@SpringBootApplication
public class Application { 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@AutoConfiguration不是必要的,因为@SpringBootApplication内部包含

票数 1
EN

Stack Overflow用户

发布于 2016-06-25 03:46:18

您的应用程序类必须如下所示:

代码语言:javascript
复制
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);
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38024482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档