2012-05-08 13 views
5

テストアプリケーションを生産性の高い(?)アプリケーションに移動し、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リクエストなどをロギングしていた古いプロジェクトは動作していましたが、もうそれはありません:(そう、初心者のブルースがたくさんあります。 !

更新 問題が解決される。答え

答えて

2

問題は解決しました。私はこれがサーブレットのコンテナ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> 

、私は明示的にServletContainerInitializerの実装をスキャンするためにTomcatを伝えるために持っていたではない、すべてが属性メタデータを経由してweb.xmlにあります-complete = "false"

+1

DOCTYPEを削除してください。これは無効です。アプリにServlet 2.3と3.0のバージョンアップされたアプリだと伝えています。 – Pidster

+0

答えを投稿した直後に私はそれを認識しました:-)コメントをいただきありがとうございます。 – user1323865

0

を確認してくださいあなたは、Tomcat Managerに行くと展開されているものを見たことがあります?それが展開されたアプリの一覧を表示する必要があります。私は、パスが変更されたと思っています。

+0

私はこれを既に行いました。そこに/ test-webappが表示されています。 http:// myhost:myport/test-webapp /はデフォルトのindex.jspを表示しますが、http:// myhost:myport/test-webapp/myrestserviceの結果は「要求されたリソース(/ test-webapp/myrestservice)が見つかりません"Tomcat7のエラーです。 – user1323865

2

実稼働環境ではどのバージョンのTomcatを使用していますか?Servlet 3.0仕様の実装にはTomcat 7が必要ですWebApplicationInitializerをサポートするために必要です。

+0

私はTomcat 7.0.17を使用しています。そして、私の以前のアプリケーションのバージョンでは、私はSpring3.0に従っており、web.xmlにはブートストラップ設定エントリがあります。しかし、私が理解する限り、Spring 3.1では、web.xmlでこの設定を行う必要はありません.WebApplicationInitializerが見つかりました。私はちょうど私のweb.xmlが以前のものだったことを覚えていました。しかし、Spring 3.1では、web.xmlの設定があまりなくても動作しないのでしょうか?ありがとう! – user1323865

+0

Joelに感謝します。私はあなたのServlet3.0仕様のヒントを使用して、作業を進める方法を見つけました。 – user1323865

0

あなたは春に新しいしているので、私はあなたが私の前の回答を見てお勧めします:

The requested resource (/) is not available

+0

ありがとうございます。私はすでに私のprobsへの答えのためにstackoverflowを検索している間、あなたの答えにぶつかった。 eclipseマーケットプレイスからSpring IDEを追加しても、私はまだ問題を解決できませんでした。 – user1323865

関連する問題