如何使Spring应用程序在不使用Spring的情况下工作呢?
在Eclipsetomcat8.5中运行此项目时,我希望url "localhost:8080/hello“显示"HelloWorld",而不是显示404
src/main/java/com.package/HelloController.java
@RestController
public class HelloController {
@RequestMapping("/hello")
public String helloWorld() {
return "Hello World";
}
}src/main/java/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()
}发布于 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的方法:
src/main/java/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[] { "/" };
}
}注:为什么投反对票?
发布于 2018-03-07 14:59:06
您可能需要WebApplicationInitializer和AnnotationConfigWebApplicationContext on start up.In 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("/");
}
}发布于 2018-03-07 15:09:15
我不确定您是否检查了web应用程序的web.xml,是否正确地设置了以下配置
<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 :如果您想访问没有war名称的URL,您可能需要检查这个Deploy war on Tomcat without the war name in the URL
https://stackoverflow.com/questions/49154810
复制相似问题