use*_*865 5 java rest spring json tomcat
一旦我将测试应用程序移动到高效(?)应用程序并开始在Tomcat上进行测试,我的基于Spring 3.1的REST服务就停止了工作.虽然显示了默认的index.jsp,但我的应用程序(http:// myhost:myport/test-webapp/myrestservice)无法访问,我得到了所请求的资源(/ test-webapp/myrestservice)不可用.
这是我做的:
控制器:
package com.test.webapp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.webap.entity.Account;
@Controller
@RequestMapping("/myrestservice")
public class AccountController{
@RequestMapping(method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Account getEntity() {
// ... <simplified>
return new Account(//...);
}
}
Run Code Online (Sandbox Code Playgroud)
Dispatch Servlet配置:
package com.test.webapp.web;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import com.test.webapp.config.AppConfig;
public class AppInit implements WebApplicationInitializer {
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
public void onStartup(ServletContext container) throws ServletException {
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(dispatcherContext));
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet(
DISPATCHER_SERVLET_NAME, new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
Run Code Online (Sandbox Code Playgroud)
配置类:
package com.test.webapp.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan( basePackages = {"com.test.webapp.controller"})
public class AppConfig{
// Nothing here. Once I start using the beans, I will put them here
}
Run Code Online (Sandbox Code Playgroud)
并且,web.xml中没有太多内容:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Test Web App</display-name>
</web-app>
Run Code Online (Sandbox Code Playgroud)
在我的项目中除此之外没有任何东西.我错过了什么吗?旧项目更像是记录传入的JSON请求等(现已被删除)正在运行,但我不再拥有它了(所以,很多新手布鲁斯.请在这里帮助我.非常感谢! !
更新 问题已解决.检查答案
问题解决了。我觉得这是一个 Servlet 容器 3.0 问题,可能是我的 Tomcat 7 配置中的某些内容混乱了。但是,我做了一些小改动web.xml
,现在效果很好:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">
<display-name>Test Web App</display-name>
</web-app>
Run Code Online (Sandbox Code Playgroud)
显然,我必须明确告诉 Tomcat 扫描 ServletContainerInitializer 的实现,并且并非所有内容都通过属性metadata-complete="false"都在 web.xml 中
归档时间: |
|
查看次数: |
1755 次 |
最近记录: |