首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Rest HelloWorld应用程序没有SpringBoot的Gradle

Spring Rest HelloWorld应用程序没有SpringBoot的Gradle
EN

Stack Overflow用户
提问于 2018-03-07 14:53:34
回答 3查看 856关注 0票数 0

如何使Spring应用程序在不使用Spring的情况下工作呢?

在Eclipsetomcat8.5中运行此项目时,我希望url "localhost:8080/hello“显示"HelloWorld",而不是显示404

src/main/java/com.package/HelloController.java

代码语言:javascript
复制
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String helloWorld() {
        return "Hello World";
    }

}

src/main/java/com.package/HelloConfig.java

代码语言:javascript
复制
public class HelloConfig {

    @Bean
    public HelloController helloController() {
        return new HelloController();
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(HelloConfig.class);
        context.getBean(HelloController.class);
    }
}

build.gradle

代码语言:javascript
复制
plugins {
    id 'java'
    id 'war'
    id 'eclipse-wtp'
}

dependencies {
    compile 'org.springframework:spring-context:5.0.3.RELEASE'
    compile 'org.springframework:spring-web:5.0.3.RELEASE'
    testCompile 'junit:junit:4.12'
}

repositories {
    mavenCentral()
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-08 08:30:15

回答我自己的问题:缺失的位是DispatcherServlet,它是负责将http请求委托给控制器的逻辑,就像我的例子中的HelloController。

基于Spring (https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet),有三种配置DispatcherServlet的方法:

  1. 在web.xml中
  2. 覆盖WebApplicationInitializer
  3. 扩展(推荐应用程序使用基于Java的配置,比如我的)

src/main/java/com.package/ServletInitializer:

代码语言:javascript
复制
public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { HelloConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

注:为什么投反对票?

票数 1
EN

Stack Overflow用户

发布于 2018-03-07 14:59:06

您可能需要WebApplicationInitializer和AnnotationConfigWebApplicationContext on start up.In onStartup方法,您可以提到应用程序的根上下文,并访问相应的控制器映射。

代码语言:javascript
复制
public class CustomWebAppInitializer implements WebApplicationInitializer {

 @Override
 public void onStartup(ServletContext container) {
  AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
  rootContext.register(RootConfiguration.class);
  ContextLoaderListener contextLoaderListener = new ContextLoaderListener(rootContext);
  container.addListener(contextLoaderListener);

  AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
  webContext.register(MvcConfiguration.class);
  DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);
  ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", dispatcherServlet);
  dispatcher.addMapping("/");
 }
}
票数 0
EN

Stack Overflow用户

发布于 2018-03-07 15:09:15

我不确定您是否检查了web应用程序的web.xml,是否正确地设置了以下配置

代码语言:javascript
复制
<servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:pathToYourSpringBeanConfig/channel-application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath: pathToYourSpringBeanConfig/channel-application-context.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

此外,您还需要提供上下文xml应用程序的位置,在其中您需要告诉上下文,如下所示:

代码语言:javascript
复制
<mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="group.*"></context:component-scan>

PS :如果您想访问没有war名称的URL,您可能需要检查这个Deploy war on Tomcat without the war name in the URL

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49154810

复制
相关文章

相似问题

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