使用Spring REST扩展现有动态项目

时间:2015-12-13 14:55:13

标签: java rest spring-mvc

我需要在现有的JSP / Servlets项目中添加REST支持。我想使用Spring因为它的社区和它的可能性。我必须实现几个功能,我相信Spring可以帮助我实现它

  1. 使用带有path / rest / *
  2. 的现有web.xml实现REST
  3. 使REST消耗并生成JSON和XML。 XML是默认的
  4. 使REST使用现有的身份验证机制
  5. 我没有在互联网上找到任何涵盖前两个陈述的帖子。有许多带有自动配置的Spring启动示例和没有REST的spring-mvc 3.x

    关于身份验证的第3个目标,我想我可以为这样的方法创建自定义注释:

    @RequestMapping("/rest/greeting")
    @MyAuthCheck(group="admin")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    ...
    

    此处显示当前代码https://github.com/anteastra/spring-rest-jsp。第三次提交 我做了以下步骤

    1. 在Eclipse中创建动态项目
    2. 为项目添加了弹簧库,我从Maven Spring Rest教程获得的库列表:

      aopalliance-1.0  
      commons-logging-1.2  
      jstl-1.2  
      spring-aop-4.2.3.RELEASE  
      spring-beans-4.2.3.RELEASE  
      spring-context-4.2.3.RELEASE  
      spring-core-4.2.3.RELEASE  
      spring-expression-4.2.3.RELEASE  
      spring-web-4.2.3.RELEASE  
      spring-webmvc-4.2.3.RELEASE  
      
    3. 将DispatcherServlet添加到web.xml

      <servlet>
           <servlet-name>rest</servlet-name>
           <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
           </servlet-class>
           <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
           <servlet-name>rest</servlet-name>
           <url-pattern>/welcome.jsp</url-pattern>
           <url-pattern>/welcome.html</url-pattern>
           <url-pattern>*.html</url-pattern>
           <url-pattern>/rest/*</url-pattern>
      </servlet-mapping>
      
    4. 添加了RestController

      package com.app;
      import java.util.concurrent.atomic.AtomicLong;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.RestController;
      import com.domain.Greeting;
      
      @RestController
      public class GreetingController {
      
          private static final String template = "Hello, %s!";
          private final AtomicLong counter = new AtomicLong();
      
          @RequestMapping("/greeting")
          public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
              return new Greeting(counter.incrementAndGet(),
                              String.format(template, name));
          }
      
      }
      
    5. 在web.xml中添加了用于组件扫描的rest-servlet

      <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd">
      
      <context:component-scan base-package="com.app" />
      
      <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
          <property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView" />
          <property name="prefix" value="/WEB-INF/jsp/" />
          <property name="suffix" value=".jsp" />
      </bean>
      

    6. 在与Spring-MVC相关的代码中还有一些额外的东西,而不是REST部分。例如,有一个@Controller

      然后我启动tomcat它的日志说所有映射都正确:

       дек 13, 2015 8:04:34 PM  org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
       INFO: Mapped URL path [/greeting] onto handler 'greetingController'
       дек 13, 2015 8:04:34 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
       INFO: Mapped URL path [/greeting.*] onto handler 'greetingController'
       дек 13, 2015 8:04:34 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
       INFO: Mapped URL path [/greeting/] onto handler 'greetingController'
      

      然后我对我得到的/ rest /问候语执行GET请求:

        

      HTTP状态404 - /WEB-INF/jsp/greeting.jsp

      我发现有人认为这是因为错过了@ResponseBody注释。 我认为Spring 4.x中不需要这个注释,但我补充道 @ResponseBody要检查:

      @RestController
      public class GreetingController {
      
          private static final String template = "Hello, %s!";
          private final AtomicLong counter = new AtomicLong();
      
          @RequestMapping("/greeting")
          public @ResponseBody Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
              return new Greeting(counter.incrementAndGet(), String.format(template, name));
          }
      }
      

      有“HTTP状态406”,此请求标识的资源只能根据请求“接受”标题生成具有不可接受特征的响应。

      好的,我修改了@RequestMapping

      @RequestMapping(value="/greeting", produces={"application/json", "application/xml"}, consumes={"application/json", "application/xml"})
      

      但我仍然有HTTP状态406 in soapUI

      如何使用Spring 4.x实现我的目标?

0 个答案:

没有答案
相关问题