当初配置springmvc的时候,因为是第一次使用springmvc,为图快捷,因而大量从网上查找入门信息。
遗憾的是,网上的配置真是五花八门,让我彻底凌乱。
至今阅读文章不少于300余篇,回头适当整理下这个。
spring mvc的mvc:annotation-driven以及日期的处理摘自
http://blog.csdn.net/xiejx618/article/details/24745207 2014-04-29
<mvc:annotation-driven />是什么意思?
参考手册http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/pdf/spring-framework-reference.pdf会讲得比较清楚
17.15 Configuring Spring MVC讲到<mvc:annotation-driven />就是
A。注册了
一个RequestMappingHandlerMapping,
一个RequestMappingHandlerAdapter
和一个ExceptionHandlerExceptionResolver
(其中包括)支持使用注解标注在Controller方法的处理请求,例如@RequestMapping ,@ExceptionHandler等等
B。它还执行以下操作:1. Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.2. Support for formatting Number fields using the @NumberFormat annotation through the ConversionService.3. Support for formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation.4. Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.5. HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values from @RequestMapping or @ExceptionHandler methods.
C。Details for B5This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:• ByteArrayHttpMessageConverter converts byte arrays.• StringHttpMessageConverter converts strings.• ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.• SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.• FormHttpMessageConverter converts form data to/from a MultiValueMap<String,String>.• Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.• MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.• AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.• RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.其实相信在大多数实际应用环境中使用mvc:annotation-driven是少数,因为一般都满足不了需求,但想快速搭配环境还是比较适合的.当使用java config,记得有文章介绍不推荐配置RequestMappingHandlerMapping和RequestMappingHandlerAdapter
如果不使用mvc:annotation-driven,日期又如何处理.
spring mvc 1. 默认是支持yyyy-MM-dd格式的字符串转换为java的java.util.Date.包括spring mvc框架本身和spring mvc支持的jackson.
2. 对于其它格式的日期的字符串与Java的Date对象相互转化,一样可分为两种情况:
2.A:一种普通请求,前台的日期字符串与后台的Java Date对象转化,此情况,应使用spring mvc本身的内置日期处理.
2.B:另一种将参数写进请求体里面,使用application/json这样的mediaType发请求,对于此情况,应使用Jackson的序列化和反序列化来处理.
一.第1种情况(不要忘了加joda-time包哦):
1.先使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")在Controller的方法参数或VO的属性使用.
2.如果不使用mvc:annotation-driven,那么使用数据绑定来处理@DateTimeFormat这样的注解.配置例子如下:
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
- <property name="webBindingInitializer">
- <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
- <property name="conversionService" ref="conversionService" />
- </bean>
- </property>
- <property name="messageConverters">
- <list>
- <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>application/json; charset=UTF-8</value>
- <value>text/html; charset=UTF-8</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
- <bean id="conversionService" class="org.springframework.format.support.DefaultFormattingConversionService"/>
二.第2种情况:1.继承定义序列化和反序列化类.例子:- public class DateJsonSerializer extends JsonSerializer<Date> {
- public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- @Override
- public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
- jsonGenerator.writeString(format.format(date));
- }
- }
- public class DateJsonDeserializer extends JsonDeserializer<Date> {
- public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- @Override
- public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
- try {
- return format.parse(jsonParser.getText());
- } catch (ParseException e) {
- throw new RuntimeException(e);
- }
- }
- }
2.在VO使用@JsonSerialize(using = DateJsonSerializer.class)和@JsonDeserialize(using = DateJsonDeserializer.class)注解(属性或方法都可以,序列化标注在get方法,反序列化标注在set方法).@JsonSerialize(using = DateJsonSerializer.class)
@JsonDeserialize(using = DateJsonDeserializer.class)
private Date createTime;
作者小注:
spring3.2开始不推荐使用setMediaTypes等直接设置这些数据, 而是通过ContentNegotiationManager(ContentNegotiationManagerFactoryBean) <bean class="ContentNegotiatingViewResolver">
<property name="contentNegotiationManager">
<bean class="ContentNegotiationManagerFactoryBean">
<property name="mediaTypes">
<props>注入即可
</property>
</bean>
</property>
</bean>
使用spring MVC构建RESTful Web Services(一):准备
摘自 2014-05-18 http://my.oschina.net/wuxianAbs/blog/266136
现在系统之间的交互越来越多,为实现跨语言访问,之前使用SOAP格式的webService,但接口开发比较复杂,且使用XML作中间语言,对XML的掌握也要求较高,最近这几年兴起了rest风格的webService调用,使用HTTP协议,接口不需要wsdl定义,数据传输格式既可以用JSON也可以用XML,简单了很多,它已经飞入寻常开发人员身边了,而且框架的选择也很多,甚至用原生的sevlet都能实现。
从spring MVC3开始,就支持REST风格的编程,配置更简单,可以全部用注解实现,这次就使用它来完成我们的功能。
一、使用框架
Spring MVC :3.2.8.RELEASE 。spring4新功能还没看,就用这个版本吧
数据库访问:待定,前期不准备用数据库,如果有精力再加吧
maven 这个不解释
二、spring MVC配置
spring MVC的配置比较简单,主要配置两处:web.xml和spring自己的配置
web.xml配置如下。有三处需要注意。
1.web.xml有两处指定了spring配置文件路径,但作用不同。
A. 在context-param中指定的路径创建的是通用的spring管理bean,用来管理我们的service层及其以下的资源、事务、aop等。
B. 在servlet中定义的DispatcherServlet是spring MVC的定义,它用来管理web层资源。因为是自家东西,集成也比较方便。web层的spring application会继承通用层的spring application,方便我们调用底层服务。
2.在定义DispatcherServlet的匹配模式的时候,不再使用常用的"*.action"或"*.do",而是使用"/",表示对所有资源的访问都会进入spring MVC,这样我们访问的时候不需要带后缀,直接以localhost:8080/restbucks/coffee这种url访问。而且后缀我们另做他用:客户端用后缀来标识想要的数据格式,我们会根据后缀返回对应格式的数据。比如请求的url带".xml"后缀表示你想要XML格式的数据,".json"后缀表示你想要JSON格式的数据,后面会讲到如何配置。
3.在正常的web项目中,我们都免不了会有jsp页面和静态资源,为了能正常访问,我们需要做一些设置。有好几种解决办法,这里使用最简单,效率也最高的一种。将不需要spring MVC处理的文件后缀在web.xml里显示标识出来,让web服务器的默认servlet处理,不走spring MVC。也可以使用spring MVC提供的功能。参看<mvc:resources/>和<mvc:default-servlet-handler/>的使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</
|