テストアプリケーションを生産的な(?)アプリケーションに移動し、Tomcatでテストを開始すると、Spring3.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(//...);
}
}
サーブレット構成のディスパッチ:
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リクエストなどをログに記録するような古いプロジェクト(現在は削除されています)は機能していましたが、もうありません:(だから、たくさんの初心者ブルース。ここで助けてください。どうもありがとう! !!
アップデート の問題が解決されました。答えを確認してください