转载请注明 http://xuantan.iteye.com/admin/blogs/1826334
利用androidannotations的Rest注解替换手工建立Spring Android RestTemplate对象,不仅可以减少很多代码,同时使工程结构更简洁、严谨,利于复用。
自己构造RestTemplate的原生写法:
HttpHeaders requestHeaders = new HttpHeaders(); List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>(); acceptableMediaTypes.add(MediaType.APPLICATION_JSON); requestHeaders.setAccept(acceptableMediaTypes); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); ResponseEntity<User[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, User[].class);
利用androidannotations的Rest注解替换后的代码:
... @RestService RestApiService restApiService; ... ... ResponseEntity<User[]> responseEntity = restApiService.getByStatus();
RestApiService的代码为:
package com.taobao.together.service; import java.util.List; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import com.googlecode.androidannotations.annotations.rest.Get; import com.googlecode.androidannotations.annotations.rest.Rest; import com.taobao.together.common.model.User; /** * 类说明 * @author xuantan.zym * @version V1.0 创建时间: 2013-3-8 下午2:33:18 */ @Rest(rootUrl = "http://10.0.2.2:8080/together/api", converters = { MappingJackson2HttpMessageConverter.class }) public interface RestApiService { @Get("/user/getByStatus.do?status=1") ResponseEntity<User[]> getUsersByStatus(); }
相关参考:
https://github.com/excilys/androidannotations/wiki/Rest-API
相关推荐
Androidannotation使用之@Rest与服务器交互的JSON数据转换
spring rest template 封装,支持文件下载,超时属性设置,http rest接口调用
spring+mybatis+rest全注解demo
注解 (annotations) 列表 @SpringBootApplication : 包含了 @ComponentScan、@Configuration 和 @EnableAutoConfiguration 注解。 其中 @ComponentScan 让 spring Boot 扫描到 Configuration 类并把它加入到程序上...
适用于Android的Spring适用于Android的Spring是旨在提供用于本机Android应用程序的Spring Framework系列项目组件的库。 行为准则该项目遵循适用于Android的Spring,适用于Android的Spring是一个旨在提供用于本地...
Building a Rest API with Spring Building a Rest API with Spring
spring + cxf + restful + soap 方便初学者很快上手。 注解描述 @Path注解的值是一个相对的URI路径,这个路径指定了该Java类的位置,例如/helloworld。在这个URI中可以包含变量,例如可以获取用户的姓名然后作为参数...
Androidannotation使用之@Rest获取资源及用户登录验证
我们也可以在Spring REST Client使用@JsonView注解。Spring提供了MappingJacksonValue 包装POJO和序列化视图设置,在发送到web服务器短的时候,我们可以使用RestTemplate来过滤JSON。现在一步一步呈现完整的示例。
NULL 博文链接:https://zhaoshijie.iteye.com/blog/1711170
Spring REST is a practical guide for designing and developing RESTful APIs using the Spring Framework. This book walks you through the process of designing and building a REST application while taking...
rest+spring+hibernate rest和spring之间用json交互
EasyRest========EasyRest is an extension to Spring-Android restTemplate that allows making rest calls using generics. it features arbitrary caching (IE: Dear server, I don't care about your opinion, I...
Spring REST Docs。 官网 Spring REST Docs API。 Spring REST Docs 开发文档。
Spring for Android Spring for Android is a library that is designed to provide components of the Spring Framework family of projects for use in native Android applications. Code of Conduct This ...
spring boot 连接数据库的例子,可以调试运行,目前流行的开发模式。
spring-android-rest-template-1.0.1.RELEASE.jar.zip
NULL 博文链接:https://topmanopensource.iteye.com/blog/1290481
5. Spring REST 6. Spring Deployment to the Cloud 7. Spring Social 8. Spring Security 9. Spring Mobile 10. Spring with other Web Frameworks 11. Data Access 12. Spring Transaction Management 13. Spring ...
同时,在3.x版本之后,它开始之初Rest风格的请求URL,为开发者提供了开发基于Restful访问规则的项目提供了帮助。 SpringData是一组技术合集。里面包含了JDBC,Data JPA,Data Redis,Data Mongodb,Data Rabbit,...