テストアプリケーションを生産性の高い(?)アプリケーションに移動し、Tomcatでテストを開始すると、Spring 3.1ベースのRESTサービスが機能しなくなりました。デフォルトのindex.jspが表示されますが、アプリケーション(http:// myhost:myport/test-webapp/myrestservice)にアクセスできず、要求されたリソース(/ test-webapp/myrestservice)を利用できません。ここでSpring 3.1 REST with JSON:動作していません
は私がやったことです:
コントローラー:
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("/");
}
}
Configurationクラス:
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リクエストなどをロギングしていた古いプロジェクトは動作していましたが、もうそれはありません:(そう、初心者のブルースがたくさんあります。 !
更新 問題が解決される。答え
DOCTYPEを削除してください。これは無効です。アプリにServlet 2.3と3.0のバージョンアップされたアプリだと伝えています。 – Pidster
答えを投稿した直後に私はそれを認識しました:-)コメントをいただきありがとうございます。 – user1323865