Embedded Jetty:选择现有的Spring MVC控制器

时间:2017-07-13 15:05:55

标签: java spring-mvc jetty

上下文

我在一个web-app(使用Play Framework)上工作,我正在尝试使用Spring MVC迁移到传统的Servlet模型。我想在一个已经存在的Jetty容器(netty)中运行嵌入式Jetty容器。

问题

我正在尝试重新使用创建的Spring上下文(包含所有应用程序bean,包括新添加的Spring MVC控制器),但是请求映射没有被选中。

我调试了Spring的Dispatcher Servlet,并且确实没有注册映射(所以它不能处理任何路径)。

尝试解决方案

这是手动Jetty设置代码:

@RequiredArgsConstructor
public class EmbeddedJetty {

    private final int port;
    private final AnnotationConfigWebApplicationContext existingContext;

    @SneakyThrows
    public void start() {
        Assert.notNull(existingContext.getBean(UserController.class));

        val server = new Server(port);
        ServletContextHandler handler = new ServletContextHandler();
        ServletHolder servlet = new ServletHolder(new DispatcherServlet(existingContext));
        handler.addServlet(servlet, "/");
        handler.addEventListener(new ContextLoaderListener(existingContext));
        server.setHandler(handler);

        server.start();
        log.info("Server started at port {}", port);
    }

}

这里的控制器被忽略了:

@Controller
public class UserController {

    @GetMapping("/users/{userId}")
    public ResponseEntity<?> getUser(@PathVariable("userId") long userId) {
        return ResponseEntity.ok("I work");
    }

}

问题

我需要做些什么才能使我的嵌入式jetty设置获取现有的控制器bean并提供映射?

4 个答案:

答案 0 :(得分:1)

我相信您缺少处理Controller内@RequestMapping请求的MVC Java Config。

所以基本上你需要添加的是一个WebMVC配置类,如:

package my.spring.config;
//necessary imported packages avoided for shortening the example
@EnableWebMvc 
@Configuration
@ComponentScan({ "my.jetty.test" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {

}

然后您需要向配置所在的AnnotationConfigWebApplicationContext表明,添加到start方法中:

webAppContext.setConfigLocation("my.spring.config");

瞧,下面(非常简单的)Controller在我的localhost上提供请求:

package my.jetty.test;
//necessary imported packages avoided for shortening the example
@Controller
public class HelloController {

    @GetMapping(value = "/")
    @ResponseBody
    public String printWelcome(HttpServletRequest request) {
        return "hello";
    }

    @GetMapping(value = "/hello/{name:.+}")
    @ResponseBody
    public String hello(@PathVariable("name") String name) {
        return "hello " + name;
    }
}

如果需要,我可以举出完整的例子。几个帮助我的链接:

编辑:The repo我的代码正常工作

答案 1 :(得分:1)

静态结构

如果您要迁移到servlet模型,您可能希望熟悉正常结构:

    .
    ├── pom.xml
    ├── README
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   ├── resources
    │   │   │   ├── spring
    │   │   └── webapp
    │   │       └── WEB-INF
    │   │           └── web.xml
    │   └── test
    │       ├── java
    │       └── resources

web.xml是j2ee服务器用于部署应用程序的核心描述符。应用程序中存在一些重要组件,在web.xml

中定义
  • 过滤
  • 的servlet
  • 听者

Sever Start

当服务器启动时,它将设置监听器以监控整个应用程序或请求的生命周期;它会设置过滤器来过滤请求;它会设置servlet来处理请求。

Spring Way

Spring是一种非常轻量级的方法,可以将许多方便的方法/实用程序集成到我们的应用程序中。它重量轻,因为它仅通过两件事附加到我们的项目中:

  • web.xml中定义spring的监听器以初始化
  • 将所有请求指向spring(适用于Spring MVC)

建议

所以,回到我们的问题。

  • 您的jetty服务器以ServletContextHandler开头,它与映射有关,但与侦听器无关(即没有spring config将是init)。您应该从WebAppContext;
  • 开始
  • 您至少应该添加web.xml来获取现有的控制器bean并提供映射;

参考

答案 2 :(得分:1)

这最终有点痛苦和重新工作,但最终的解决方案是确保在启动DispatcherServlet之前existingContext NOT 已启动并刷新。

答案 3 :(得分:-1)

在您的Web配置文件中尝试此操作

@ComponentScan(basePackages = {"com.our.controller"})