Spring boot HTTP調用其他服務

1.GET請求

1.1Client代碼

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoClient {
    public String getUserTotalAmount(){
        Map<String,String> map=new HashMap<String,String>();
        map.put("name","123");
        map.put("password","123");
        URI uri = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/spring/test")
                .queryParam("jsonString",JSON.toJSONString(map))
                .queryParam("token","12122222111")
                .build().encode().toUri();
        RestTemplate restTemplate=new RestTemplate();
        String data=restTemplate.getForObject(uri,String.class);
        System.out.println(data);
        return null;
    }
    public static void main(String[] args){
        UserInfoClient c=new UserInfoClient();
        c.getUserTotalAmount();
    }
}
1.2 Service 代碼
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/spring")
public class Test {
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String testSpringBoot(@RequestParam String jsonString,@RequestParam String token){
        System.out.println(jsonString);
        System.out.println(token);
        /*
         *邏輯處理
         */
        return "Spring Boot 測試成功!";
    }
}

2.POST請求

2.1Client代碼


import com.alibaba.fastjson.JSON;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoClient {

    public String getUserTotalAmount(){
        Map<String,String> map=new HashMap<String,String>();
        map.put("name","123");
        map.put("password","123");
        String url="http://localhost:8081/spring/test";
        //設置請求頭信息
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        //設置body部分
        HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(map),headers);
        RestTemplate restTemplate=new RestTemplate();
        ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        System.out.println(result.getBody());
        return null;
    }
    public static void main(String[] args){
        UserInfoClient c=new UserInfoClient();
        c.getUserTotalAmount();
    }
}
2.2 Service代碼

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/spring")
public class Test {
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public String testSpringBoot(@RequestBody UserBean userBean){
        System.out.println(userBean);
        /*
         *邏輯處理
         */
        return "Spring Boot 測試成功!";
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章