java远程调用

第一种

1 引用maven

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

 

2 部分代码

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@FeignClient("mn-test")
public interface SendNotifier {
    @RequestMapping(value = {"/controlMap/testDemo"}, method = {RequestMethod.POST}, produces = {"application/json; charset=UTF-8"})
    ResultVo<Object> testDemo(@RequestBody Student stu);
    
}

2第二种方式

实例代码

 import org.springframework.web.client.RestTemplate;
 @Service
 public class ServiceA{
 @Autowired
    private RestTemplate restTemplate;
 public void fun(){
 ResponseEntity<String> responseEntity = restTemplate.postForEntity(
         "http://monitor-serviceName/control1/requestMethod1", obj, Map.class);
//返回string
 String body = responseEntity.getBody(); 
 JSONObject bodyObj = JSON.parseObject(body);
//返回对象
//Map<String, Map<String, String>> calendar = new HashMap<>();
//calendar = responseEntity.getBody();

}

 

posted @ 2020-02-27 09:22  一人一见  阅读(524)  评论(0)    收藏  举报