Spring MVC 3.0 RESTful controller
1. Controller 代码非常简单
- package org.pprun.hjpetstore.web.rest;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.pprun.hjpetstore.persistence.jaxb.Products;
- import org.pprun.hjpetstore.service.rest.HjpetstoreService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Required;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.servlet.ModelAndView;
- /**
- * A RESTful controller supplies search products by keyword which will be exposed to public access.
- * The access authenticated by api-key as Google/Yahoo/Amazon Web Service.
- *
- * @author <a href="mailto:quest.run@gmail.com">pprun</a>
- */
- @Controller
- public class HjpetstoreController extends BaseController {
- private static final Log log = LogFactory.getLog(HjpetstoreController.class);
- private HjpetstoreService hjpetstoreService;
- @Required
- @Autowired
- public void setHjpetstoreService(HjpetstoreService hjpetstoreService) {
- this.hjpetstoreService = hjpetstoreService;
- }
- /**
- * RESTful match path '/products/{keyword}' with apikey as request parameter.
- *
- * <p>
- * For example: user pprun <br />
- * {@code
- * curl -u pprun:pprunpprun -H 'Accept: application/xml' 'http://localhost:8080/hjpetstore/rest/products/dog?apikey=bc7163dab8eb79a9867b4604b46b0328e9ace555ef5d9526e1fcd748f9864bf85d59e97c044a2d9795736753c2b0d77cd085eb05d854e5849f42f37f85851220&page=1&max=100'
- * }
- *
- * @param apiKey
- * @param keyword
- * @return
- */
- @RequestMapping(value = "/products/{keyword}", method = RequestMethod.GET)
- public ModelAndView getProductsByKeyword(
- @RequestParam("apikey") String apiKey,
- @RequestParam("page") int page,
- @RequestParam("max") int max,
- @PathVariable("keyword") String keyword) {
- if (log.isDebugEnabled()) {
- log.debug("HjpetstoreController is processing request for keyword: " + keyword);
- }
- Products products = hjpetstoreService.searchProductList(apiKey, keyword, page, max);
- ModelAndView mav = new ModelAndView("products");
- mav.addObject(products);
- return mav;
- }
- }
2. Spring context xml
- <!--
- To enable autodetection of such annotated controllers, you add component scanning to your configuration.
- The controllers are autodetected POJOs labeled with the @Controller annotation.
- -->
- <context:component-scan base-package="org.pprun.hjpetstore.web.rest"/>
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <!-- 19.9.2 HTTP Message Conversion
- several main media type converters have been registered,
- but if we overwrite tihs property, we have to list all our need-->
- <property name="messageConverters">
- <list>
- <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>application/xml</value>
- <value>text/xml</value>
- <!-- curl set this type automatically -->
- <value>application/x-www-form-urlencoded</value>
- </list>
- </property>
- <property name="marshaller" ref="jaxb2Marshaller" />
- <property name="unmarshaller" ref="jaxb2Marshaller" />
- </bean>
- <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
- </list>
- </property>
- </bean>
- <!-- view resolver -->
- <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
- <property name="mediaTypes">
- <map>
- <entry key="xml" value="application/xml"/>
- <entry key="html" value="text/html"/>
- </map>
- </property>
- <property name="viewResolvers">
- <list>
- <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1"/>
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/WEB-INF/jsp/shop/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </list>
- </property>
- </bean>
- <!-- searchProducts rest GET -->
- <bean name="products" class="org.springframework.web.servlet.view.xml.MarshallingView">
- <constructor-arg ref="jaxb2Marshaller" />
- </bean>
- <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
- <property name="classesToBeBound">
- <list>
- <value>org.pprun.hjpetstore.persistence.jaxb.Products</value>
- </list>
- </property>
- we can depend on the xsd file for automatically validation
- <property name="schema" value="classpath:org/springframework/oxm/schema.xsd"/>
- </bean>-->
Spring MVC 3.0 RESTful controller相关推荐
- 使用Spring MVC 4构建Restful服务
使用Spring MVC 4构建RESTful服务相对于其它框架来说,有很多优势.首先,Spring MVC 4作为Spring的框架之一,可以很好地与Spring进行集成.其次,Spring MVC ...
- Spring mvc 3.0 入门及应用
[一]Spring应用 Spring 支持json格式的jar jackson-all-1.7.3 http://jackson.codehaus.org/ Spring MVC 3.x annot ...
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法 1. 直接 PrintWriter 输出 2. 使用 JSP 视图 3. 使用Spring内置的支持 // Spring MVC 配置 <b ...
- 【Spring MVC学习】详解spring mvc 3.0常用注解
Spring mvc的注解功能 1.@Controller--表示控制器 举例: @Controller public class SoftCreateController extendsSimple ...
- Spring MVC 解读——@Autowired、@Controller、@Service从原理层面来分析
Spring MVC 解读--@Autowired 一.@Autowired 作为一个Spring开发者对@Autowired注解必定是非常了解了, 顾名思义自动装配,应该是Spring会自动将我们标 ...
- 解决 spring mvc 3.0 结合 hibernate3.2 使用tx:annotation-driven声明式事务无法提交的问题(转载)...
1.问题复现 spring 3.0 + hibernate 3.2 spring mvc使用注解方式:service使用@service注解 事务使用@Transactional 事务配置使用 Jav ...
- Spring源码深度解析(郝佳)-学习-源码解析-Spring MVC(三)-Controller 解析
在之前的博客中Spring源码深度解析(郝佳)-学习-源码解析-Spring MVC(一),己经对 Spring MVC 的框架做了详细的分析,但是有一个问题,发现举的例子不常用,因为我们在实际开发项 ...
- 从零开始学习 ASP.NET MVC 1.0 (三) Controller/Action 深入解析与应用实例 【转】
一.摘要 一个Url请求经过了Routing处理后会调用Controller的Action方法. 中间的过程是怎样的? Action方法中返回ActionResult对象后,如何到达View的? 本文 ...
- Spring MVC 常用注解之 Controller 篇
Shopping Without Vision 的 project 终于告一段落,teamwork 非常给力,中间也遇到很多困难,还好都一起克服了.这是做的第二个 Spring MVC 项目了,对这 ...
最新文章
- 人工智能vs人类智能小传
- 分子生物学之蛋白质与氨基酸
- oracle分区交换有啥好处,分区交换的速度为什么快?
- 【maven插件】versions-maven-plugin : 管理版本号
- idea如何连接本地mysql_IDEA如何连接MYSQL
- 城市大轰炸(洛谷P1830题题解,Java语言描述)
- 一种简单的可控并发粒度的TaskScheduler的实现
- oracle 创建SDO_Geometry表
- 快速修剪技巧_20个相见恨晚的CAD画图神技巧,让你效率飞升!
- AspNetForum 论坛整改:添加显IP功能及IP所属地
- Android 父类super.onDestroy();的有关问题
- 单元覆盖测试排除某些类烧苗_汽车嵌入式软件测试——嵌入式软件测试概述
- 怎么自己制作证件照?如何在线制作证件照电子版?
- VM虚拟机安装黑群晖教程
- 华为 OSPF虚链路出现环路了,如何解决?
- unit在matlab中啥意思,unit8(matlab中uint8函数)
- python游戏开发(贪吃蛇游戏、五子棋游戏、大球吃小球游戏)
- 软件测试怎么样才能提高自己的效率?
- intptr_t详解
- 丹佛斯变频器al13故障_丹佛斯变频器常见故障维修
热门文章
- ubuntu下apt相关操作
- 数据结构专题(一):1.3.顺序表插入删除
- html5技术英文论文参考文献,英文论文的参考文献范例(精选8篇)
- java基础应用_Java基础(应用篇)
- torch.nn与torch.nn.functional
- 2.3 指数加权平均
- Pandas 文本数据方法 join( )
- html中文本框冒号对齐,html5 冒号分隔符对齐的实现,
- Spring Cloud与微服务学习总结(13)——云原生趋势下,微服务的拆分粒度如何把握?
- Java基础学习总结(146)——开发人员日志实践规范