2016-11-23 8 views
2

Java Configurationを使用してSpring 4 RESTFulサービスを作成し、Tomcatにデプロイしようとしています。しかし、私はエンドポイントを打つことができません。私は何が欠けていますか?これは私が持っているものです。Java Configurationを使用してSpring RESTFulサービスを作成する方法は?

私はセッターとゲッターを持つGreeting POJOを持っています。

public class Greeting { 
    private BigInteger id; 
    private String text; 

    //setters and getters 
} 

私はグリーティングコントローラを持っています。

@Controller 
public class GreetingController { 

    private static BigInteger nextId; 
    private static Map<BigInteger, Greeting> greetingMap; 

    //have some code to store Greetings in greetingMap 

    @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    public ResponseEntity<Collection<Greeting>> getGreetings() { 
     Collection<Greeting> greetings = greetingMap.values(); 
     return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK); 
    } 

} 

私はConfigurationクラスを持っています。

@Configuration 
@ComponentScan(basePackages = "org.example") 
    public class GreetingConfiguration { 

} 

そして、これは私のsrc /メイン/ webappの/ WEB-INF/web.xmlの私は、Tomcatにデプロイする

<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring Application</display-name> 

    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextClass</param-name> 
      <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
     </init-param> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>org.example.ws.configuration.GreetingConfiguration</param-value> 
     </init-param> 
     <init-param> 
      <param-name>dispatchOptionsRequest</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>org.example.ws.configuration.GreetingConfiguration</param-value> 
    </context-param> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

です。 Tomcatはokを開始します。私はそれをヒットしようとしたhttp://localhost:8080/api/greetings、それは私に404を与える。何が足りないですか?

ありがとうございます!

+0

にしてController' @ '交換してみてください'@ RestController'あなたのアプリケーションのウェルカムページにアクセスできますか?ルートコンテキストはありませんか? url-patternには '/ *'を使います。 – Isukthar

+0

あなたは 'GreetingConfiguration'に問題があると思います。 –

答えて

0

は、私はあなたの残りの部分の方法

@Controller 
    public class GreetingController { 

     private static BigInteger nextId; 
     private static Map<BigInteger, Greeting> greetingMap; 

     //have some code to store Greetings in greetingMap 

     @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
     @ResponseBody 
     public ResponseEntity<Collection<Greeting>> getGreetings() { 
      Collection<Greeting> greetings = greetingMap.values(); 
      return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK); 
     } 

    } 

に@ResponseBodyを追加し、あなたがXMLとJavaの設定を組み合わせている

1

あなたのpom.xmlにジャクソンのデータバインディングの依存関係を持っていることを確認する必要があると思います。だから、あなたは二つの方法のいずれかによって、コントローラ注釈をピックアップして春を伝える必要があります:コンフィギュレーションクラスの

  1. @EnableWebMvc
  2. または、XML
関連する問題