用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

it绿萝    -  云代码空间

——

java B2B2C Springcloud仿淘宝电子商城系统-Feign 的简单使用及传参方式

2018-12-29|419阅||

摘要:Feign 简介 Feign 是简化Java HTTP客户端开发的工具。它使用注解的方式将HTTP的URL封装成接口,每个URL对应一个接口,大大简化了HTTP客户端的开发。 需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 壹零叁八柒

Feign 简介


Feign 是简化Java HTTP客户端开发的工具。它使用注解的方式将HTTP的URL封装成接口,每个URL对应一个接口,大大简化了HTTP客户端的开发。


需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 壹零叁八柒柒肆六二六


1.添加依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
</dependency>
<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-httpclient</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
</dependency>

定义API
import feign.Headers;
import feign.Param;
import feign.RequestLine;


import java.util.List;


/**
* Create by zdran@gmail.com on 2018/3/20
*
*/
//Headers 注解:配置请求头信息
@Headers({"Accept: application/json", "Content-Type: application/json"})
public interface UserApi {
/**
* RequestLine 注解:请求的方法与
* GET 请求方法,遵循RESTful风格
* @return
*/
@RequestLine("GET /user/all")
List<User> getAllUser();


/**
* restful方式传参
* @param name
* @return
*/
@RequestLine("GET /user/{name}")
User getByName(@Param("name") St


/**
* url方式传参数
* @param id
* @return
*/
@RequestLine("GET /user/id?id={i
User getById(@Param("id") String


/**
* post 传参,传复杂类型
* @param user
*/
@RequestLine("POST /user/add")
void addUser(User user);
}

定义实现API的controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
* Create by zdran@gmail.com on 2018/3/20
 *
 */
@RestController
@RequestMapping(value = "/user", produces = "application/json")
public class UserController {


   @GetMapping(value = "/all")
   List<User> getAll(){
       List<User> users = new ArrayList<>();
       User user = new User();
       user.setName("获取所有用户");
       users.add(user);
       return users;
   }
   @GetMapping(value = "/{name}")
   User getByName(@PathVariable String name){
       User user = new User();
       user.setName("获取用户:"+name);
       return user;
   }
   @GetMapping(value = "/id")
   User getById(String id){
       User user = new User();
       user.setName("获取用户:"+id);
       return user;
   }
   @PostMapping(value = "/add")
   void addUser(@RequestBody User user){


   }

使用
import feign.Feign;
import feign.httpclient.ApacheHttpClient;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


/**
 * Create by zdran@gmail.com on 2018/3/20
 *
 */
@Controller
public class ClientController {
    private static final String apiBaseUrl = "http://localhost:8080/feign";
    UserApi userApi = Feign.builder()
           .client(new ApacheHttpClient())
            .encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder())
            .target(UserApi.class, apiBaseUrl);


    @GetMapping(value = "/client/user/{name}")
    public User getUserInfo(@PathVariable String name){
        return userApi.getByName(name);
    }


}
java B2B2C springmvc mybatis仿淘宝电子商城系统 
顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: it绿萝
    • 等级: 高级设计师
    • 积分: 5730
    • 代码: 0 个
    • 文章: 192 篇
    • 随想: 0 条
    • 访问: 15 次
    • 关注

    人气代码

      最新提问

        站长推荐