struts restful风格http接口
来源:互联网 发布:黑米软件 贴吧 编辑:程序博客网 时间:2024/06/15 09:26
新需求告一段落,总结一下前阶段问题。在这之前不得不吐槽一下,明明之前说好的远程接口服务用dubbo+zookeeper,都tm写完了又说不用了,换http接口,换就换吧还什么restful,记得上次接触restful还是n年前在的mvc下面用过,现在乍一听在struts下面用这个,差点愁坏了某些人,因为组里没一个人用过,无奈花了大半天各种论坛各种逛,但是这方面的资料寥寥无几,而且各个帖子的最终来源好像都一样。介于已经应用成功,那我就再总结一下。
首先修改pom里面的jar包,主要涉及rest,json等几个插件
<!-- 配置struts依赖包 --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><exclusions><exclusion><groupId>com.sun</groupId><artifactId>tools</artifactId></exclusion>
这里特别说明添加rest服务必须要有struts2-convention-plugin插件包而convention必须要有asm包
<!-- <exclusion><artifactId>asm</artifactId><groupId>asm</groupId></exclusion><exclusion><artifactId>asm-commons</artifactId><groupId>asm</groupId></exclusion> --></exclusions><version>${struts.version}</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-web</artifactId></exclusion></exclusions><version>${struts.version}</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-config-browser-plugin</artifactId><version>${struts.version}</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-json-plugin</artifactId><version>${struts.version}</version></dependency><dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-rest-plugin</artifactId><version>${struts.version}</version></dependency>然后开始配置struts
<!-- struts2 restful start--> <!--指定控制器类的后缀为 Controller--> <constant name="struts.convention.action.suffix" value="Controller"/> <constant name="struts.convention.action.mapAllMatches" value="true"/> <!-- 指定 支持 REST 风格的 Action 所在包继承的父包 --> <constant name="struts.convention.default.parent.package" value="rest-default"/> <!-- struts2 restful end-->注意因为系统有多个拦截器,而我们的接口服务不能独立出系统,要呈现的效果就是不用登录系统也能正常提供数据服务,为了避免同之前的struts系统请求冲突,避免服务被无辜拦截,我们需要在web.xml中的cas拦截器中给它开一个特权路径。
ok,配置到此结束,下面开始实例,此处省略实体类、service服务,
这里注意接口命名必须以Controller 结尾且要继承、实现ActionSupport、ModelDriven
public class TestController extends ActionSupport implements ModelDriven<Object>{ private int id; public int getId() {return id;}public void setId(int id) {this.id = id;}
public void getUserInfo() {user = userService.findUserbyName(userName);String str = JSONUtil.object2json(user);writeResponse(str);}// 实现 ModelDriven 接口必须实现的 getModel 方法public Object getModel() { return null; } }介于美观性,我们可以把包括Id在内的所有参数放到一个新的TestSupport里面,变成TestController extends TestSupport ,用TestSupport继承ActionSupport这个Controller就会非常整洁.
参考url: http://localhost:8080/web/services/user/1/getUserInfo?username=admin
阅读全文
0 0
- struts restful风格http接口
- restful接口风格
- RESTful风格接口
- restful风格接口
- Restful风格的接口设计
- Restful是一种非常优美的http接口设计风格及设计规范
- SpringBoot-创建RESTful风格的 http接口访问jpa 来操作数据库
- 理解并设计rest/restful风格接口
- Restful风格API接口开发springMVC篇
- 理解并设计rest/restful风格接口
- Restful风格API接口开发springMVC篇
- Restful风格API接口开发springMVC篇
- Restful风格API接口开发springMVC篇
- RestFul风格WebService接口的定义
- Restful风格API接口开发springMVC篇
- Restful风格API接口开发springMVC篇
- 理解并设计rest/restful风格接口
- 理解并设计rest/restful风格接口
- acess denied for user 'Administrator'@'localhost' (using password: YES) 解决办法
- Effective Java 对于所有对象都通用的方法 12.考虑实现Comparable(可以比较)接口
- 数组
- C++ string提取空格之间的substring的方法
- 全国首家互联网银行——微众银行内推啦!!!技术产品各个岗位可供选择!!!最后一周!!!抓紧时间啦!!!
- struts restful风格http接口
- Java语言使用注解处理器生成代码 —— 第一部分:注解类型
- 使用HttpServletRequestWrapper 实现防止xss攻击和sql注入
- lateinit 和 by lazy 的区别(Kotlin)
- angular4中引入svg
- 设计模式----结构型模式
- 小白的第一次flask编程
- JavaScript的隐式类型转换浅析
- 主存与Cache的地址映射方式