如何使用Java配置创建Spring RESTFul服务?

时间:2016-11-23 09:25:03

标签: java spring rest spring-mvc

我正在尝试使用Java配置创建Spring 4 RESTFul服务并部署到Tomcat。但我无法击中终点。我错过了什么?这就是我所拥有的。

我有一个问候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 / main / webapp / WEB-INF / web.xml

<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。 Tomcat开始了。我试图点击它http://localhost:8080/api/greetings,它给了我404.我错过了什么?

谢谢!

2 个答案:

答案 0 :(得分:1)

您正在组合XML和Java配置。因此,您需要告诉Spring通过以下两种方法之一来获取控制器注释:

  1. 配置类中的@EnableWebMvc
  2. 或者,用XML

答案 1 :(得分:0)

我认为你需要将@ResponseBody添加到你的休息方法

 @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);
      }

    }

并检查你的pom.xml中是否有jackson数据绑定依赖