【问题标题】:Spring 3.1 REST with JSON: Not working带有 JSON 的 Spring 3.1 REST:不起作用
【发布时间】:2012-05-17 05:41:08
【问题描述】:

一旦我将我的测试应用程序转移到生产 (?) 应用程序并开始在 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(//...);
}
}

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 没有太多内容:

<!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>

在我的项目中除此之外别无其他。我错过了什么吗?旧项目更像是记录传入的 JSON 请求等(现在已被删除)正在工作,但我不再拥有它了 :( 所以,很多新手忧郁。请在这里帮助我。非常感谢! !

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

【问题讨论】:

    标签: java json spring rest tomcat


    【解决方案1】:

    问题已解决。我觉得这是一个 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>
    

    显然,我必须通过 metadata-complete="false"

    属性明确告诉 Tomcat 扫描 ServletContainerInitializer 的实现,而不是所有内容都在 web.xml 中

    【讨论】:

    • 删除 DOCTYPE,它是无效的 - 你告诉应用它是一个 Servlet 2.3 AND 3.0 版本的应用。
    • 我在发布答案后立即意识到 :-) 感谢您的评论。
    【解决方案2】:

    由于您是 Spring 新手,我建议您看看我之前的回答:

    The requested resource (/) is not available

    【讨论】:

    • 谢谢。在搜索 stackoverflow 以寻找我的问题的答案时,我已经碰到了你的答案。虽然我从 eclipse 市场添加了 Spring IDE,但我仍然无法解决我的问题。
    【解决方案3】:

    您在生产环境中使用的是什么版本的 Tomcat?看来您需要 Tomcat 7 才能获得支持 WebApplicationInitializer 所需的 Servlet 3.0 规范实现。

    【讨论】:

    • 我在 Tomcat 7.0.17 上。是的,在我以前的应用程序版本中,我遵循 Spring3.0 并且 web.xml 具有引导配置条目。但是,据我了解,使用 Spring 3.1,我不需要在 web.xml 中进行此配置 - 它应该已经找到了 WebApplicationInitializer。我只记得我的 web.xml 是如何更早的,切换到它使这个应用程序工作。但是对于 Spring 3.1,它不应该在 web.xml 中没有这么多配置的情况下工作吗?谢谢!
    • 感谢 Joel,我使用了您的 Servlet3.0 规范提示,并找到了让这些东西正常工作的方法。
    【解决方案4】:

    您是否尝试过进入 Tomcat 管理器并查看已部署的内容?它应该显示已部署应用程序的列表。我认为路径已经改变。

    【讨论】:

    猜你喜欢
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    相关资源
    最近更新 更多