retrofit:一套RESTful架构的Android(Java)客户端实现。
好处:
4.0.0
com.zzg
resultful
war
0.0.1-SNAPSHOT
resultful Maven Webapp
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE
org.springframework.boot
spring-boot-starter-web
junit
junit
3.8.1
test
io.springfox
springfox-swagger2
2.2.2
io.springfox
springfox-swagger-ui
2.2.2
package com.zzg.resultful;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class Application implements EmbeddedServletContainerCustomizer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
/**
* EmbeddedServletContainerCustomizer接口的未实现方法
* 指定容器的启动端口,之后再浏览器输入localhost:8081/swagger-ui.html即可
*/
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8081);
}
}
说明:
package com.zzg.resultful.domain;
public class Hotel {
private int id;
private String hotelname;
public Hotel() {
}
public Hotel(int id, String hotelname) {
this.id = id;
this.hotelname = hotelname;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getHotelname() {
return hotelname;
}
public void setHotelname(String hotelname) {
this.hotelname = hotelname;
}
}
package com.zzg.resultful.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zzg.resultful.domain.Hotel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/hotel")
@Api("HotelController相关api")
public class HotelController {
@ApiOperation("获取酒店Hotel信息:getHotelWithQueryParameter")
@RequestMapping(value="/getHotelWithQueryParameter",method=RequestMethod.GET)
public Hotel getHotelWithQueryParameter(@RequestParam("hotelname") String hotelname) {
if(hotelname.equals("nana")){
return new Hotel(777, "假日酒店");
}
return new Hotel(1314, "玫瑰酒店");
}
@ApiOperation("获取酒店Hotel信息:getHotelList")
@RequestMapping(value="/getHotelList",method=RequestMethod.POST)
public List getHotelList() {
List hotelList = new ArrayList();
hotelList.add(new Hotel(1314, "玫瑰酒店"));
hotelList.add(new Hotel(2046, "2046酒店"));
return hotelList;
}
@ApiOperation("获取酒店Hotel信息:getHotelListWithBody")
@RequestMapping(value="/getHotelListWithBody",method=RequestMethod.POST)
public List getHotelListWithBody(@RequestBody Hotel hotel) {
List hotelList = new ArrayList();
if(hotel.getHotelname().equals("武林酒店")){
hotelList.add(new Hotel(13141, "玫瑰酒店1"));
hotelList.add(new Hotel(20461, "2046酒店1"));
return hotelList;
}
hotelList.add(new Hotel(1314, "玫瑰酒店"));
hotelList.add(new Hotel(2046, "2046酒店"));
return hotelList;
}
}
说明:该类提供了三个方法,也是将来customerresultful服务远程调用的三个方法。
4.0.0
com.zzg
customerrestful
0.0.1-SNAPSHOT
jar
customerrestful
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE
org.springframework.boot
spring-boot-starter-web
junit
junit
3.8.1
test
io.springfox
springfox-swagger2
2.2.2
io.springfox
springfox-swagger-ui
2.2.2
com.squareup.retrofit
retrofit
1.9.0
package com.zzg.customerrestful.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit.RestAdapter;
@Configuration
public class RestAdapterConfig {
/**
* 获取RestAdapter单例Bean
* @return
*/
@Bean
public RestAdapter getRestAdapter(){
/**
* setEndpoint("http://localhost:8081"):指定基本的URL,
* API接口中的URL是相对于该URL的路径的,
* 不能少了协议名,例如写成:localhost:8081就不行
*/
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint("http://localhost:8081")
.build();
return adapter;
}
}
说明:
package com.zzg.customerrestful.api;
import java.util.List;
import com.zzg.customerrestful.domain.Hotel;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.POST;
import retrofit.http.Query;
public interface HotelAPI {
/**
* GET请求带查询参数
*/
@GET("/hotel/getHotelWithQueryParameter")
public Hotel getHotelWithQueryParameter(@Query("hotelname") String hotelname);
/**
* POST请求
*/
@POST("/hotel/getHotelList")
public List getHotelList();
/**
* POST请求,带参数JavaBean
*/
@POST("/hotel/getHotelListWithBody")
public List getHotelListWithBody(@Body Hotel hotel);
}
说明:
package com.zzg.customerrestful.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit.RestAdapter;
@Configuration
public class HotelAPIConfig {
@Autowired
private RestAdapter adapter;
@Bean
public HotelAPI getHotelAPI(){
return adapter.create(HotelAPI.class);
}
}
说明:
经过以上步骤后,之后就可以直接在其他类中注入HotelAPI实例来像普通的bean进行操作了。
package com.zzg.customerrestful.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzg.customerrestful.api.HotelAPI;
import com.zzg.customerrestful.domain.Hotel;
@Service
public class UserService {
@Autowired
private HotelAPI hotelApi;
public Hotel getHotelFromMyboot2WithQueryParameter(String hotelname){
return hotelApi.getHotelWithQueryParameter(hotelname);
}
public List getHotelFromMyboot2List(){
return hotelApi.getHotelList();//测试post请求
}
public List getHotelFromMyboot2ListWithBody(Hotel hotel){
return hotelApi.getHotelListWithBody(hotel);//测试post请求
}
}
说明:
package com.zzg.customerrestful.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zzg.customerrestful.domain.Hotel;
import com.zzg.customerrestful.service.UserService;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@ApiOperation("获取酒店信息,测试GETWithQueryParameter")
@RequestMapping(value="/getHotelWithQueryParameter",method=RequestMethod.GET)
public Hotel getHotel(@RequestParam("hotelname") String hotelname) {
return userService.getHotelFromMyboot2WithQueryParameter(hotelname);
}
@ApiOperation("获取酒店信息,测试POST")
@RequestMapping(value="/getHotelList",method=RequestMethod.GET)
public List getHotelList() {
return userService.getHotelFromMyboot2List();
}
@ApiOperation("获取酒店信息,测试POST")
@RequestMapping(value="/getHotelListWithBody",method=RequestMethod.GET)
public List getHotelListWithBody() {
return userService.getHotelFromMyboot2ListWithBody(new Hotel(888, "武林酒店"));
}
}
测试:
首先,启动服务resultful,浏览器输入"localhost:8081/swagger-ui.html"可以查看服务是否启动成功。
其次,启动服务customerresultful,浏览器输入"localhost:8080/swagger-ui.html",再进行相应接口的测试即可。
效果展示: