• Spring MVC 3.0 RESTful controller


    1. Controller 代码非常简单

     

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

     

     

    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.   
    7. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  
    8.   
    9. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    10. <!--         19.9.2 HTTP Message Conversion  
    11.         several main media type converters have been registered,  
    12.         but if we overwrite tihs property, we have to list all our need-->  
    13.      
    14.     <property name="messageConverters">  
    15.         <list>  
    16.             <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">  
    17.                 <property name="supportedMediaTypes">  
    18.                     <list>  
    19.                         <value>application/xml</value>  
    20.                         <value>text/xml</value>  
    21.                         <!-- curl set this type automatically -->  
    22.                         <value>application/x-www-form-urlencoded</value>  
    23.                     </list>  
    24.                 </property>  
    25.                 <property name="marshaller" ref="jaxb2Marshaller" />  
    26.                 <property name="unmarshaller" ref="jaxb2Marshaller" />  
    27.             </bean>  
    28.             <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>  
    29.         </list>  
    30.     </property>  
    31. </bean>  
    32.   
    33. <!-- view resolver -->  
    34. <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
    35.     <property name="mediaTypes">  
    36.         <map>  
    37.             <entry key="xml" value="application/xml"/>  
    38.             <entry key="html" value="text/html"/>  
    39.         </map>  
    40.     </property>  
    41.     <property name="viewResolvers">  
    42.         <list>  
    43.             <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1"/>  
    44.   
    45.             <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    46.                 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
    47.                 <property name="prefix" value="/WEB-INF/jsp/shop/"/>  
    48.                 <property name="suffix" value=".jsp"/>  
    49.             </bean>  
    50.         </list>  
    51.     </property>  
    52. </bean>  
    53.   
    54.   
    55. <!-- searchProducts rest GET -->  
    56. <bean name="products"  class="org.springframework.web.servlet.view.xml.MarshallingView">  
    57.     <constructor-arg ref="jaxb2Marshaller" />  
    58. </bean>  
    59.   
    60.   
    61. <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">  
    62.     <property name="classesToBeBound">  
    63.         <list>  
    64.             <value>org.pprun.hjpetstore.persistence.jaxb.Products</value>  
    65.         </list>  
    66.     </property>  
    67.      we can depend on the xsd file for automatically validation  
    68.      <property name="schema" value="classpath:org/springframework/oxm/schema.xsd"/>  
    69. </bean>-->  
  • 相关阅读:
    kafka集群搭建
    更改:把redis替换成kafka
    mysql+canal+kafka+elasticsearch构建数据查询平台
    zookeeper集群搭建
    另类--kafka集群中jmx端口设置
    kafka集群中jmx端口设置
    使用zookeeper报错 stat is not executed because it is not in the whitelist. envi is not executed because it is not in the whitelist.
    使用python的kazoo模块连接zookeeper实现最基本的增删改查
    Maven之阿里云镜像仓库配置
    通过yum安装maven
  • 原文地址:https://www.cnblogs.com/chenying99/p/2540538.html
Copyright © 2020-2023  润新知