spring restful实践(一)

来源:互联网 发布:cda数据分析师考试条件 编辑:程序博客网 时间:2024/06/06 20:25

最近学习spring greenhouse项目,发现是用restful方式,和原来使用xml配置文件的方式差别比较大,写下来备忘。

1.创建spring mvc项目

为了方便我使用了eclipse spring插件直接建了个项目

2.改造web.xml

加入

<!-- Java-based annotation-driven Spring container definition --><context-param>    <param-name>contextClass</param-name>    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param>
修改contextConfigLocation,原来对应的是配置文件,现在对应的是配置目录:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --><context-param><param-name>contextConfigLocation</param-name><!-- <param-value>/WEB-INF/spring/root-context.xml</param-value> --><param-value>com.yunzu.oauthtest.config</param-value></context-param>
另外servlet的配置修改成以下方式:

<!-- Processes application requests --><servlet><servlet-name>appServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><!-- <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> --><param-value></param-value></init-param><load-on-startup>1</load-on-startup></servlet>

2.pom.xml修改

添加cglib

                 <dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.2.2</version></dependency> 

3.配置目录修改

src/main/java下创建包com.yunzu.oauthtest.config,对应到上面的contextConfigLocation

然后创建ComponentConfig类,内容如下:

package com.yunzu.oauthtest.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Profile;@Configuration@ComponentScan(basePackages="com.yunzu.oauthtest")public class ComponentConfig {/** * Properties to support the 'embedded' mode of operation. */@Configurationstatic class Embedded {}}
重点是两个注解,相当于xml配置的<context:component-scan base-package="com.yunzu.oauthtest" />
有了这些项目就可以运行起来了,但却不能访问到任何可用的页面,这是因为还没有配置mvc

为配置mvc,需要创建WebConfig类,最简单配置如下:

package com.yunzu.oauthtest.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.servlet.view.JstlView;@Configuration@EnableWebMvcpublic class WebConfig  extends WebMvcConfigurerAdapter {private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/views/";    private static final String VIEW_RESOLVER_SUFFIX = ".jsp";       @Bean    public ViewResolver viewResolver() {        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();        viewResolver.setViewClass(JstlView.class);        viewResolver.setPrefix(VIEW_RESOLVER_PREFIX);        viewResolver.setSuffix(VIEW_RESOLVER_SUFFIX);        return viewResolver;    }}
这里实现了view resolver,有了这个配置,就可以正常的看到页面了
注意要extends WebMvcConfigurerAdapter,这样才能由系统调用addInterceptors等接口

原创粉丝点击