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等接口
- spring restful实践(一)
- spring restful实践(二)
- 一、Spring-boot设置restful
- RESTful最佳实践——一、引言
- spring实践一
- Spring Framework研究(一)RESTFUL
- Spring RESTful服务接收和返回JSON最佳实践
- Spring RESTful服务接收和返回JSON最佳实践
- Spring RESTful服务接收和返回JSON最佳实践
- Spring RESTful服务接收和返回JSON最佳实践
- 基于restful风格的maven项目实践(融合spring)
- Spring RESTful服务接收和返回JSON最佳实践
- 【实践笔记】Spring MVC中Restful API使用 Swagger2 构建
- 使用Spring RESTful服务接收和返回JSON最佳实践
- RESTful服务最佳实践——(一)
- restful--spring
- Spring RESTful
- RESTful HTTP的实践
- linux内核中打开文件 及属性控制
- 问题:Failed to install *.apk on device *: timeout
- 逐浪CMS建站专家发哥:新浪网改版的三言两语
- 提高你的Java代码质量吧:推荐在复杂字符串操作中使用正则表达式
- 阿里分布式数据库服务原理与实践
- spring restful实践(一)
- 应用程序池模板永久性缓存初始化失败
- WPF - DispatcherHelper
- arm-2009q1-203-arm-none-linux-gnueabi 安装
- php与js的区别
- 微信活动策划七个执行智慧
- FTP文件传输格式
- 嵌入式Modbus TCP通讯模块设计
- 机房收费重构版总结