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

时间:2018-03-07 14:53:34

标签: java spring rest tomcat

如何在不使用Spring Boot的情况下使我的Spring Rest HelloWorld应用程序正常运行?

在eclipse中运行tomcat 8.5中的这个项目时,我希望url“localhost:8080 / hello”显示“HelloWorld”,但它显示404

的src /主/爪哇/ com.package / HelloController.java

@RestController
public class HelloController {

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

}

的src /主/爪哇/ com.package / HelloConfig.java

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

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()
}

3 个答案:

答案 0 :(得分:0)

启动时可能需要WebApplicationInitializer和AnnotationConfigWebApplicationContext。在onStartup方法中,您可以提及应用程序的根上下文并访问其上的控制器映射。

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("/");
 }
}

答案 1 :(得分:0)

我不确定您是否已检查web.xml以获取Web应用程序,并且您已正确设置以下配置

<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应用程序的位置,您需要告诉您的上下文,如下所示:

<mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="group.*"></context:component-scan>

PS:如果你想访问没有战争名称的URL,你可能想检查一下 Deploy war on Tomcat without the war name in the URL

答案 2 :(得分:0)

回答我自己的问题:缺少的是DispatcherServlet,负责将http请求委托给控制器的逻辑,比如我的示例中的HelloController。

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

  1. in web.xml
  2. 覆盖WebApplicationInitializer
  3. 扩展AbstractAnnotationConfigDispatcherServletInitializer(推荐用于像我这样基于Java的配置的应用程序)
  4. 的src /主/爪哇/ com.package / ServletInitializer:

    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[] { "/" };
        }
    }
    

    注意:为什么要进行投票?