gpt4 book ai didi

java - 带有 JSON : Not working 的 Spring 3.1 REST

转载 作者:搜寻专家 更新时间:2023-10-31 20:14:41 25 4
gpt4 key购买 nike

一旦我将我的测试应用程序移动到一个生产性(?)应用程序并开始在 Tomcat 上进行测试,我基于 Spring 3.1 的 REST 服务就停止工作了。虽然显示了默认的 index.jsp,但我的应用程序 (http://myhost:myport/test-webapp/myrestservice) 无法访问,我得到 The requested resource (/test-webapp/myrestservice) is not available.

这是我做的:

Controller :

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

调度 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 请求等(现在已删除)正在运行,但我不再拥有它了:(所以,很多新手忧郁症。请在这里帮助我。非常感谢! !

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

最佳答案

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

关于java - 带有 JSON : Not working 的 Spring 3.1 REST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10503698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com