1. Controller 代码非常简单

Java代码  
  1. package org.pprun.hjpetstore.web.rest;
  2. import org.apache.commons.logging.Log;
  3. import org.apache.commons.logging.LogFactory;
  4. import org.pprun.hjpetstore.persistence.jaxb.Products;
  5. import org.pprun.hjpetstore.service.rest.HjpetstoreService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Required;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.servlet.ModelAndView;
  14. /**
  15. * A RESTful controller supplies search products by keyword which will be exposed to public access.
  16. * The access authenticated by api-key as Google/Yahoo/Amazon Web Service.
  17. *
  18. * @author <a href="mailto:quest.run@gmail.com">pprun</a>
  19. */
  20. @Controller
  21. public class HjpetstoreController extends BaseController {
  22. private static final Log log = LogFactory.getLog(HjpetstoreController.class);
  23. private HjpetstoreService hjpetstoreService;
  24. @Required
  25. @Autowired
  26. public void setHjpetstoreService(HjpetstoreService hjpetstoreService) {
  27. this.hjpetstoreService = hjpetstoreService;
  28. }
  29. /**
  30. * RESTful match path '/products/{keyword}' with apikey as request parameter.
  31. *
  32. * <p>
  33. * For example: user pprun <br />
  34. * {@code
  35. * curl -u pprun:pprunpprun -H 'Accept: application/xml' 'http://localhost:8080/hjpetstore/rest/products/dog?apikey=bc7163dab8eb79a9867b4604b46b0328e9ace555ef5d9526e1fcd748f9864bf85d59e97c044a2d9795736753c2b0d77cd085eb05d854e5849f42f37f85851220&page=1&max=100'
  36. * }
  37. *
  38. * @param apiKey
  39. * @param keyword
  40. * @return
  41. */
  42. @RequestMapping(value = "/products/{keyword}", method = RequestMethod.GET)
  43. public ModelAndView getProductsByKeyword(
  44. @RequestParam("apikey") String apiKey,
  45. @RequestParam("page") int page,
  46. @RequestParam("max") int max,
  47. @PathVariable("keyword") String keyword) {
  48. if (log.isDebugEnabled()) {
  49. log.debug("HjpetstoreController is processing request for keyword: " + keyword);
  50. }
  51. Products products = hjpetstoreService.searchProductList(apiKey, keyword, page, max);
  52. ModelAndView mav = new ModelAndView("products");
  53. mav.addObject(products);
  54. return mav;
  55. }
  56. }

2. Spring context xml

Xml代码  
  1. <!--
  2. To enable autodetection of such annotated controllers, you add component scanning to your configuration.
  3. The controllers are autodetected POJOs labeled with the @Controller annotation.
  4. -->
  5. <context:component-scan base-package="org.pprun.hjpetstore.web.rest"/>
  6. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
  7. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  8. <!--         19.9.2 HTTP Message Conversion
  9. several main media type converters have been registered,
  10. but if we overwrite tihs property, we have to list all our need-->
  11. <property name="messageConverters">
  12. <list>
  13. <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
  14. <property name="supportedMediaTypes">
  15. <list>
  16. <value>application/xml</value>
  17. <value>text/xml</value>
  18. <!-- curl set this type automatically -->
  19. <value>application/x-www-form-urlencoded</value>
  20. </list>
  21. </property>
  22. <property name="marshaller" ref="jaxb2Marshaller" />
  23. <property name="unmarshaller" ref="jaxb2Marshaller" />
  24. </bean>
  25. <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
  26. </list>
  27. </property>
  28. </bean>
  29. <!-- view resolver -->
  30. <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  31. <property name="mediaTypes">
  32. <map>
  33. <entry key="xml" value="application/xml"/>
  34. <entry key="html" value="text/html"/>
  35. </map>
  36. </property>
  37. <property name="viewResolvers">
  38. <list>
  39. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1"/>
  40. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  41. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  42. <property name="prefix" value="/WEB-INF/jsp/shop/"/>
  43. <property name="suffix" value=".jsp"/>
  44. </bean>
  45. </list>
  46. </property>
  47. </bean>
  48. <!-- searchProducts rest GET -->
  49. <bean name="products"  class="org.springframework.web.servlet.view.xml.MarshallingView">
  50. <constructor-arg ref="jaxb2Marshaller" />
  51. </bean>
  52. <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  53. <property name="classesToBeBound">
  54. <list>
  55. <value>org.pprun.hjpetstore.persistence.jaxb.Products</value>
  56. </list>
  57. </property>
  58. we can depend on the xsd file for automatically validation
  59. <property name="schema" value="classpath:org/springframework/oxm/schema.xsd"/>
  60. </bean>-->

Spring MVC 3.0 RESTful controller相关推荐

  1. 使用Spring MVC 4构建Restful服务

    使用Spring MVC 4构建RESTful服务相对于其它框架来说,有很多优势.首先,Spring MVC 4作为Spring的框架之一,可以很好地与Spring进行集成.其次,Spring MVC ...

  2. Spring mvc 3.0 入门及应用

    [一]Spring应用 Spring 支持json格式的jar jackson-all-1.7.3  http://jackson.codehaus.org/ Spring MVC 3.x annot ...

  3. Spring MVC 3.0 返回JSON数据的方法

    Spring MVC 3.0 返回JSON数据的方法 1. 直接 PrintWriter 输出 2. 使用 JSP 视图 3. 使用Spring内置的支持 // Spring MVC 配置 <b ...

  4. 【Spring MVC学习】详解spring mvc 3.0常用注解

    Spring mvc的注解功能 1.@Controller--表示控制器 举例: @Controller public class SoftCreateController extendsSimple ...

  5. Spring MVC 解读——@Autowired、@Controller、@Service从原理层面来分析

    Spring MVC 解读--@Autowired 一.@Autowired 作为一个Spring开发者对@Autowired注解必定是非常了解了, 顾名思义自动装配,应该是Spring会自动将我们标 ...

  6. 解决 spring mvc 3.0 结合 hibernate3.2 使用tx:annotation-driven声明式事务无法提交的问题(转载)...

    1.问题复现 spring 3.0 + hibernate 3.2 spring mvc使用注解方式:service使用@service注解 事务使用@Transactional 事务配置使用 Jav ...

  7. Spring源码深度解析(郝佳)-学习-源码解析-Spring MVC(三)-Controller 解析

    在之前的博客中Spring源码深度解析(郝佳)-学习-源码解析-Spring MVC(一),己经对 Spring MVC 的框架做了详细的分析,但是有一个问题,发现举的例子不常用,因为我们在实际开发项 ...

  8. 从零开始学习 ASP.NET MVC 1.0 (三) Controller/Action 深入解析与应用实例 【转】

    一.摘要 一个Url请求经过了Routing处理后会调用Controller的Action方法. 中间的过程是怎样的? Action方法中返回ActionResult对象后,如何到达View的? 本文 ...

  9. Spring MVC 常用注解之 Controller 篇

    Shopping Without Vision 的 project 终于告一段落,teamwork 非常给力,中间也遇到很多困难,还好都一起克服了.这是做的第二个 Spring MVC  项目了,对这 ...

最新文章

  1. 人工智能vs人类智能小传
  2. 分子生物学之蛋白质与氨基酸
  3. oracle分区交换有啥好处,分区交换的速度为什么快?
  4. 【maven插件】versions-maven-plugin : 管理版本号
  5. idea如何连接本地mysql_IDEA如何连接MYSQL
  6. 城市大轰炸(洛谷P1830题题解,Java语言描述)
  7. 一种简单的可控并发粒度的TaskScheduler的实现
  8. oracle 创建SDO_Geometry表
  9. 快速修剪技巧_20个相见恨晚的CAD画图神技巧,让你效率飞升!
  10. AspNetForum 论坛整改:添加显IP功能及IP所属地
  11. Android 父类super.onDestroy();的有关问题
  12. 单元覆盖测试排除某些类烧苗_汽车嵌入式软件测试——嵌入式软件测试概述
  13. 怎么自己制作证件照?如何在线制作证件照电子版?
  14. VM虚拟机安装黑群晖教程
  15. 华为 OSPF虚链路出现环路了,如何解决?
  16. unit在matlab中啥意思,unit8(matlab中uint8函数)
  17. python游戏开发(贪吃蛇游戏、五子棋游戏、大球吃小球游戏)
  18. 软件测试怎么样才能提高自己的效率?
  19. intptr_t详解
  20. 丹佛斯变频器al13故障_丹佛斯变频器常见故障维修

热门文章

  1. ubuntu下apt相关操作
  2. 数据结构专题(一):1.3.顺序表插入删除
  3. html5技术英文论文参考文献,英文论文的参考文献范例(精选8篇)
  4. java基础应用_Java基础(应用篇)
  5. torch.nn与torch.nn.functional
  6. 2.3 指数加权平均
  7. Pandas 文本数据方法 join( )
  8. html中文本框冒号对齐,html5 冒号分隔符对齐的实现,
  9. Spring Cloud与微服务学习总结(13)——云原生趋势下,微服务的拆分粒度如何把握?
  10. Java基础学习总结(146)——开发人员日志实践规范