微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 带有JSON的Spring 3.1 REST:不工作

一旦我将测试应用程序移动到高效(?)应用程序并开始在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() {
            // ... 

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

    }
}

配置类:

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
}

并且,web.xml中没有太多内容

stems,Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

display-name>Test Web Appdisplay-name>

在我的项目中除此之外没有任何东西.我错过了什么吗?旧项目更像是记录传入的JSON请求等(现已被删除)正在运行,但我不再拥有它了(所以,很多新手布鲁斯.请在这里帮助我.非常感谢! !

更新
问题已解决.检查答案

最佳答案
您在生产环境中使用的是什么版本的Tomcat?看起来你需要Tomcat 7来获得支持WebApplicationInitializer所需的Servlet 3.0规范实现.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐