eclipse 使用maven 集成springboot + retrofit分析

哈哈 阅读:177 2021-03-31 21:21:54 评论:0

retrofit:一套RESTful架构的Android(Java)客户端实现。

好处:

  • 基于注解
  • 提供JSON to POJOPOJO to JSON网络请求(POST,GET,PUT,DELETE等)封装

  • 可以看做是对HttpClient的再次封装
1、为了做测试,建立了一个新的springboot项目"resultful",项目结构如下:

1.1 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>com.zzg</groupId> 
  <artifactId>resultful</artifactId> 
  <packaging>war</packaging> 
  <version>0.0.1-SNAPSHOT</version> 
  <name>resultful Maven Webapp</name> 
  <url>http://maven.apache.org</url> 
  <parent>   
        <groupId>org.springframework.boot</groupId>   
        <artifactId>spring-boot-starter-parent</artifactId>   
        <version>1.4.3.RELEASE</version>   
    </parent>   
   
    <dependencies>   
        <dependency>   
            <groupId>org.springframework.boot</groupId>   
            <artifactId>spring-boot-starter-web</artifactId>   
        </dependency>   
        <dependency>   
            <groupId>junit</groupId>   
            <artifactId>junit</artifactId>   
            <version>3.8.1</version>   
            <scope>test</scope>   
        </dependency> 
        <!--引入swagger  -->   
        <dependency>   
           <groupId>io.springfox</groupId>   
           <artifactId>springfox-swagger2</artifactId>   
           <version>2.2.2</version>   
        </dependency>   
        <dependency>   
           <groupId>io.springfox</groupId>   
           <artifactId>springfox-swagger-ui</artifactId>   
           <version>2.2.2</version>   
        </dependency>     
    </dependencies>  
</project> 

1.2 Application.java
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); 
    } 
 
} 

说明:

  • 实现了EmbeddedServletContainerCustomizer接口,并实现了其方法customize(ConfigurableEmbeddedServletContainer container),指定了该服务的启动端口是8081,这样在服务myboot(启动端口:8080)启动时,就不会存在端口冲突问题了.



1.3 Hotel.java
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; 
	} 
} 


1.4 HotelController.java
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<Hotel> getHotelList() { 
        List<Hotel> hotelList = new ArrayList<Hotel>(); 
        hotelList.add(new Hotel(1314, "玫瑰酒店")); 
        hotelList.add(new Hotel(2046, "2046酒店")); 
        return hotelList; 
    } 
     
    @ApiOperation("获取酒店Hotel信息:getHotelListWithBody") 
    @RequestMapping(value="/getHotelListWithBody",method=RequestMethod.POST) 
    public List<Hotel> getHotelListWithBody(@RequestBody Hotel hotel) { 
        List<Hotel> hotelList = new ArrayList<Hotel>(); 
        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服务远程调用的三个方法。

2、新建另一个springboot 项目“customerresultful”,项目结构如下:


2.1 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
	<modelVersion>4.0.0</modelVersion> 
 
	<groupId>com.zzg</groupId> 
	<artifactId>customerrestful</artifactId> 
	<version>0.0.1-SNAPSHOT</version> 
	<packaging>jar</packaging> 
 
	<name>customerrestful</name> 
	<url>http://maven.apache.org</url> 
 
	 <parent>   
        <groupId>org.springframework.boot</groupId>   
        <artifactId>spring-boot-starter-parent</artifactId>   
        <version>1.4.3.RELEASE</version>   
    </parent>   
   
    <dependencies>   
        <dependency>   
            <groupId>org.springframework.boot</groupId>   
            <artifactId>spring-boot-starter-web</artifactId>   
        </dependency>   
        <dependency>   
            <groupId>junit</groupId>   
            <artifactId>junit</artifactId>   
            <version>3.8.1</version>   
            <scope>test</scope>   
        </dependency> 
        <!--引入swagger  -->   
        <dependency>   
           <groupId>io.springfox</groupId>   
           <artifactId>springfox-swagger2</artifactId>   
           <version>2.2.2</version>   
        </dependency>   
        <dependency>   
           <groupId>io.springfox</groupId>   
           <artifactId>springfox-swagger-ui</artifactId>   
           <version>2.2.2</version>   
        </dependency>    
        <!-- 引入retrofit --> 
          <dependency> 
           <groupId>com.squareup.retrofit</groupId> 
              <artifactId>retrofit</artifactId> 
              <version>1.9.0</version> 
          </dependency>  
    </dependencies>  
</project> 

说明:引入retrofit1.9.0依赖,与2.0的差别很大。

2.2  RestAdapterConfig.java
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; 
    } 
 
}

说明:

  • 使用 @[email protected] 构建RestAdapter单例
  • 在构建的过程中,一定要有setEndpoint("http://localhost:8081")方法,该方法指定了基本的URL(即:指定协议+IP+port)

2.3  HotelAPI.java
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<Hotel> getHotelList(); 
     
    /** 
     * POST请求,带参数JavaBean 
     */ 
    @POST("/hotel/getHotelListWithBody") 
    public List<Hotel> getHotelListWithBody(@Body Hotel hotel); 
     
} 

说明:

  • 该接口指定了调用远程服务的方法的基本路径与参数以及返回值等
  • 路径都是相对路径,相对于setEndpoint("http://localhost:8081")指定的路径
  • 方式有@GET/@POST[email protected][email protected]
  • 传递参数在get方式中可以直接将参数连到URL上去
  • 传递参数使用@Query(服务被调用方使用@RequestParam接收),路径中的传递参数使用@PathRestful风格),还可以直接[email protected](服务被调用方使用@RequestBody接收),[email protected][email protected]
  • 依旧要建一个Hotel类,最好与resultful中的相同,不同也没关系
2.4 HotelAPIConfig.java
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); 
    } 
} 

说明:

  • 使用 @[email protected] 构建HotelAPI单例。
  • HotelAPI接口实例是由RestAdapter来构建的,所以需要注入了RestAdapter

经过以上步骤后,之后就可以直接在其他类中注入HotelAPI实例来像普通的bean进行操作了。


2.5 UserService.java
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<Hotel> getHotelFromMyboot2List(){ 
        return hotelApi.getHotelList();//测试post请求 
    } 
     
    public List<Hotel> getHotelFromMyboot2ListWithBody(Hotel hotel){ 
        return hotelApi.getHotelListWithBody(hotel);//测试post请求 
    } 
 
} 

说明:

  • service中注入了HotelAPI实例,使用该实例调用接口方法
  • 其实,retrofit的接口的注入和使用与mybatis的注解方式的mapper接口的使用相似

2.6、UserController.java
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<Hotel> getHotelList() { 
        return userService.getHotelFromMyboot2List(); 
    } 
     
    @ApiOperation("获取酒店信息,测试POST") 
    @RequestMapping(value="/getHotelListWithBody",method=RequestMethod.GET) 
    public List<Hotel> getHotelListWithBody() { 
        return userService.getHotelFromMyboot2ListWithBody(new Hotel(888, "武林酒店")); 
    } 
} 

测试:

首先,启动服务resultful,浏览器输入"localhost:8081/swagger-ui.html"可以查看服务是否启动成功。

其次,启动服务customerresultful,浏览器输入"localhost:8080/swagger-ui.html",再进行相应接口的测试即可。


效果展示:








声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号