Springboot 接收参数

1接收问号?传参如:
http://localhost:8088/user/list?username=zhangsan&pwd=12345&action=Login

@GetMapping("/login")
public R login(@RequestParam("username") String username, @RequestParam("pwd") String pwd, @RequestParam("action") String action)

或者不用注解,但是参数名称必须和url参数名保持一致

(String username, String pwd, String action)

2接收Restful风格传参,如:
login/{username}/{pwd}/{action}

public R login(@PathVariable("username")  String username,@PathVariable("pwd")  String pwd,@PathVariable("action")  String action)

@PathVariable是从一个url模板里面来填充(绑定 URL 占位符到功能处理方法的参数上,主要实现RESTFULL风格的请求)


OPENFEIGN调用第三方接口
1.导入依赖

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-openfeignartifactId>
    <version>2.2.5.RELEASEversion>
dependency>

2.开启FeignClient

@SpringBootApplication
@EnableFeignClients
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

3.写接口

@FeignClient(url = "192.168.1.109:8088/user", name = "apitest")
public interface ApiFeign {

    @RequestMapping(value = "/list/{username}/{pwd}/{action}/",method = RequestMethod.GET)
    JSONObject login(@PathVariable("username") String username, @PathVariable("pwd") String pwd, @PathVariable("action") String action);
    
}

4.使用接口

JSONObject check = apiFeign.check("张三","123456","Login");

RestTemplate调用第三方接口
GET方法

//调用方
R r = restTemplate.getForObject("http://localhost:8088/user/list?role={admin}", R.class, "admin");

//接收方
@GetMapping("/list")
public R listUser(User user) 

POST方法

//调用方
ResponseEntity<R> responseEntity = restTemplate.postForEntity("http://localhost:8080/user/add", user, R.class);

//接收方
@PostMapping("/add")
public R add(@RequestBody User user)

你可能感兴趣的:(spring,boot,后端,java)