Spring 3 supports RESTful API/APP configuration examples

Source: Internet
Author: User
Tags xml example

RESTful configuration example supporting APP & API Mode
XML example:
<Beans xmlns: context = http://www.springframework.org/schema/context
... Default-autowire = "byName">
<! -ByName, byType, autodetect -->
<Context: component-scan base-package = "com. **. controller"/>
<Bean class = "org. springframework. web. servlet. mvc. annotation. DefaultAnnotationHandlerMapping"/>
<Bean class = "org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter"/>
 
<Bean class = "org. springframework. web. servlet. view. ContentNegotiatingViewResolver">
<Property name = "ignoreAcceptHeader" value = "true"/>
<Property name = "defaultContentType" value = "text/html"/>
<Property name = "mediaTypes">
<Map>
<Entry key = "json" value = "application/json"/>
<Entry key = "xml" value = "application/xml"/>
</Map>
</Property>
<Property name = "favorParameter" value = "false"/>
<Property name = "viewResolvers">
<List>
<Bean class = "org. springframework. web. servlet. view. BeanNameViewResolver"/>
<Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "viewClass" value = "org. springframework. web. servlet. view. JstlView"/>
<Property name = "prefix" value = "/WEB-INF/jsp/"/>
<Property name = "suffix" value = ". jsp"> </property>
</Bean>
</List>
</Property>

<Property name = "defaultViews">
<List>
<! -- For application/json -->
<Bean class = "org. springframework. web. servlet. view. json. MappingJacksonJsonView"/>
<! -- For application/xml -->
<Bean class = "org. springframework. web. servlet. view. xml. MarshallingView">
<Property name = "marshaller">
<Bean class = "org. springframework. oxm. xstream. xstreampolicaller"/>
<! -This is a very important column loader that can easily implement bean <=> xml, need to download additional (http://xstream.codehaus.org/download.html), it is easier to use than JAXB suite->
</Property>
</Bean>
</List>
</Property>
</Bean>
 
2. Exception of RESTful configuration in API Mode
Spring 3 Servlet + HttpMessageConverter + Annotation
-StringHttpMessageConverter: Text converter
-FormHttpMessageConverter: Form converter (application/x-www-form-urlencoded, ampped to MultiValueMap <String, String>)
-MarshallingHttpMessageConverter: XML converter (marshaller)
-MappingJacksonHttpMessageConverter: JSON converter (via Jackson's ObjectMapper
-AtomFeedHttpMessageConverter: ATOM converter (via ROME's Feed API)
-RssChannelHttpMessageConverter: RSS converter (via ROME's Feed API)
XML example:
<Bean class = "org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter">
<Property name = "messageConverters">
<List>
<Ref bean = "jsonConverter"/>
<Ref bean = "marshallingConverter"/>
<Ref bean = "atomConverter"/>
</List>
</Property>
</Bean>
<Bean id = "jsonConverter" class = "org. springframework. http. converter. json. MappingJacksonHttpMessageConverter">
<Property name = "supportedMediaTypes" value = "application/json"/> <
/Bean>
 
<Bean id = "marshallingConverter" class = "org. springframework. http. converter. xml. MarshallingHttpMessageConverter">
<Constructor-arg ref = "jaxbMarshaller"/>
<Property name = "supportedMediaTypes" value = "application/xml"/>
</Bean>
<Bean id = "jaxbMarshaller" class = "org. springframework. oxm. jaxb. Jaxb2Marshaller">
<Property name = "classesToBeBound">
<List>
<Value> com. company. project. bean. EntityA </value>
<Value> com. company. project. bean. EntityB </value>
</List>
</Property>
</Bean>
<Bean id = "atomConverter" class = "org. springframework. http. converter. feed. AtomFeedHttpMessageConverter">
<Property name = "supportedMediaTypes" value = "application/atom + xml"/>
</Bean>
Controller code example:
@ RequestMapping (method = RequestMethod. GET, value = "/emp/{id}", headers = "Accept = application/json ")
Public @ ResponseBody EntityA getEntityA (@ PathVariable String id ){... Return EntityA ;}
 
2. Support for RESTful configuration exceptions in APP Mode
 
Spring 3 Servlet + MVC + Annotation
-ContentNegotiatingViewResolver: Resource representation negotiation
-BeanNameViewResolver: Common view resolver based on bean name as spring-like.
-UrlBasedViewResolver: Spring MVC view same as former Spring version (JSP for text/html)
-InternalResourceViewResolver: Mostly like to UrlBasedViewResolver.
-MappingJacksonJsonView: JSON view
-MarshallingView: XML view (via XStreamMarshaller or Jaxb2Marshaller, etc)
XML configuration example:
<Beans... Xmlns: p = http://www.springframework.org/schema/p xmlns: context = "http://www.springframework.org/schema/context "... Default-autowire = "byName"
<Context: component-scan base-package = "com. **. controller"/>
<Bean class = "org. springframework. web. servlet. mvc. annotation. DefaultAnnotationHandlerMapping"/>
<Bean class = "org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter"/>
<Bean class = "org. springframework. web. servlet. view. ContentNegotiatingViewResolver">
<Property name = "ignoreAcceptHeader" value = "true"/>
<Property name = "defaultContentType" value = "text/html"/>
<Property name = "mediaTypes">
<Map>
<Entry key = "json" value = "application/json"/>
<Entry key = "html" value = "text/html"/>
<Entry key = "xml" value = "application/xml"/>
</Map>
</Property>
<Property name = "favorParameter" value = "false"/>
 
<Property name = "viewResolvers">
<List>
<Bean class = "org. springframework. web. servlet. view. BeanNameViewResolver"/>
<Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "viewClass" value = "org. springframework. web. servlet. view. JstlView"/>
<Property name = "prefix" value = "/WEB-INF/pages"/> <property name = "suffix" value = ". jsp"> </property>
</Bean>
</List>
</Property>
 
<Property name = "defaultViews">
<List>
<Bean class = "org. springframework. web. servlet. view. json. MappingJacksonJsonView"/>
<Bean class = "org. springframework. web. servlet. view. xml. MarshallingView">
<Property name = "marshaller">
<Bean class = "org. springframework. oxm. xstream. xstreampolicaller"/>
<! -It needs the jar libaray: xstream. jar (http://xstream.codehaus.org/download.html) -->
</Property>
</Bean>
</List>
</Property>
</Bean>
<Bean id = "messageSource" class = "org. springframework. context. support. ResourceBundleMessageSource" p: basename = "i18n/messages"/>
 
Controller code example:
@ PATH ("/{id }")
Public ModelAndView show (@ PathVariable java. lang. Integer id) throws Exception {
UserInfo userInfo = manger. get (id );
Return new ModelAndView ("/userinfo/edit", "userInfo", userInfo );
Author: "The technical diary of the watcher and six .."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.