client Project, spring web, server.port = 8080
server Project, Spring web, lobok, server.port = 9090
package com.example.client.controller;
import com.example.client.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/client")
public class ApiController {
@Autowired
private RestTemplateService restTemplateService;
public ApiController(RestTemplateService restTemplateService) {
this.restTemplateService = restTemplateService;
}
@GetMapping("/hello")
public String getHello() {
return restTemplateService.hello();
}
}
package com.example.client.service;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Service
public class RestTemplateService {
// http://localhost/api/server/hello
// response
public String hello() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/server/hello")
.encode()
.build()
.toUri();
System.out.println(uri.toString());
RestTemplate restTemplate = new RestTemplate();
// getForObject 실행되는 순간이 클라이언트에서 http에서 서버로 붙는 시간
// String result = restTemplate.getForObject(uri, String.class);
ResponseEntity<String> result = restTemplate.getForEntity(uri, String.class);
System.out.println(result.getStatusCode());
System.out.println(result.getBody());
return result.getBody();
}
}
package com.example.client.dto;
public class UserResponse {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "UserResponse{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.example.client.controller;
import com.example.client.dto.UserResponse;
import com.example.client.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/client")
public class ApiController {
@Autowired
private RestTemplateService restTemplateService;
public ApiController(RestTemplateService restTemplateService) {
this.restTemplateService = restTemplateService;
}
@GetMapping("/hello")
public UserResponse getHello() {
return restTemplateService.hello();
}
}
package com.example.server.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private int age;
}
package com.example.server.contoller;
import com.example.server.dto.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/server")
public class ServerApiController {
@GetMapping("/hello")
public User hello() {
User user = new User();
user.setName("steve");
user.setAge(10);
return user;
}
}
package com.example.client.service;
import com.example.client.dto.UserResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Service
public class RestTemplateService {
// http://localhost/api/server/hello
// response
public UserResponse hello() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/server/hello")
.queryParam("name","steve")
.queryParam("age", 10)
.encode()
.build()
.toUri();
System.out.println(uri.toString());
RestTemplate restTemplate = new RestTemplate();
// getForObject 실행되는 순간이 클라이언트에서 http에서 서버로 붙는 시간
// String result = restTemplate.getForObject(uri, String.class);
ResponseEntity<UserResponse> result = restTemplate.getForEntity(uri, UserResponse.class);
System.out.println(result.getStatusCode());
System.out.println(result.getBody());
return result.getBody();
}
}
package com.example.client.service;
import com.example.client.dto.UserResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Service
public class RestTemplateService {
// http://localhost/api/server/hello
// response
public UserResponse hello() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/server/hello")
.queryParam("name","client")
.queryParam("age", 25)
.encode()
.build()
.toUri();
System.out.println(uri.toString());
RestTemplate restTemplate = new RestTemplate();
// getForObject 실행되는 순간이 클라이언트에서 http에서 서버로 붙는 시간
// String result = restTemplate.getForObject(uri, String.class);
ResponseEntity<UserResponse> result = restTemplate.getForEntity(uri, UserResponse.class);
System.out.println(result.getStatusCode());
System.out.println(result.getBody());
return result.getBody();
}
}
package com.example.server.contoller;
import com.example.server.dto.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/server")
public class ServerApiController {
@GetMapping("/hello")
public User hello(@RequestParam String name, @RequestParam int age) {
User user = new User();
user.setName(name);
user.setAge(age);
return user;
}
}
package com.example.client.service;
import com.example.client.dto.UserRequest;
import com.example.client.dto.UserResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Service
public class RestTemplateService {
// http://localhost/api/server/hello
// response
public UserResponse hello() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/server/hello")
.queryParam("name","client")
.queryParam("age", 25)
.encode()
.build()
.toUri();
System.out.println(uri.toString());
RestTemplate restTemplate = new RestTemplate();
// getForObject 실행되는 순간이 클라이언트에서 http에서 서버로 붙는 시간
// String result = restTemplate.getForObject(uri, String.class);
ResponseEntity<UserResponse> result = restTemplate.getForEntity(uri, UserResponse.class);
System.out.println(result.getStatusCode());
System.out.println(result.getBody());
return result.getBody();
}
public UserResponse post() {
// http:localhost:9090/api/server/user/{userId}/name/{userName}
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("api/server/user/{userId}/name/{userName}")
.encode()
.build()
.expand(100, "steve") // 순서대로 괄호랑 매치가 된다, ","로 구분하여 작성
.toUri();
System.out.println(uri);
//http body -> object -> object mapper -> json -> rest template -> http body json
UserRequest req = new UserRequest();
req.setName("steve");
req.setAge(10);
RestTemplate restTemplate = new RestTemplate();
// (uri, request object, response object)
ResponseEntity<UserResponse> response = restTemplate.postForEntity(uri, req, UserResponse.class);
System.out.println(response.getStatusCode());
System.out.println(response.getHeaders());
System.out.println(response.getBody());
return response.getBody();
}
}
package com.example.client.controller;
import com.example.client.dto.UserResponse;
import com.example.client.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/client")
public class ApiController {
@Autowired
private RestTemplateService restTemplateService;
public ApiController(RestTemplateService restTemplateService) {
this.restTemplateService = restTemplateService;
}
@GetMapping("/hello")
public UserResponse getHello() {
return restTemplateService.post();
}
}
package com.example.server.contoller;
import com.example.server.dto.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/api/server")
public class ServerApiController {
@GetMapping("/hello")
public User hello(@RequestParam String name, @RequestParam int age) {
User user = new User();
user.setName(name);
user.setAge(age);
return user;
}
@PostMapping("/user/{userId}/name/{userName}")
public User post(@RequestBody User user, @PathVariable int userId, @PathVariable String userName) {
log.info("userId : {}, userName : {}", userId, userName);
log.info("client request : {}",user);
return user;
}
}
package com.example.client.controller;
import com.example.client.dto.UserResponse;
import com.example.client.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/client")
public class ApiController {
@Autowired
private RestTemplateService restTemplateService;
public ApiController(RestTemplateService restTemplateService) {
this.restTemplateService = restTemplateService;
}
@GetMapping("/hello")
public UserResponse getHello() {
restTemplateService.post();
return new UserResponse();
}
}
package com.example.client.service;
import com.example.client.dto.UserRequest;
import com.example.client.dto.UserResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Service
public class RestTemplateService {
// http://localhost/api/server/hello
// response
public UserResponse hello() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/server/hello")
.queryParam("name","client")
.queryParam("age", 25)
.encode()
.build()
.toUri();
System.out.println(uri.toString());
RestTemplate restTemplate = new RestTemplate();
// getForObject 실행되는 순간이 클라이언트에서 http에서 서버로 붙는 시간
// String result = restTemplate.getForObject(uri, String.class);
ResponseEntity<UserResponse> result = restTemplate.getForEntity(uri, UserResponse.class);
System.out.println(result.getStatusCode());
System.out.println(result.getBody());
return result.getBody();
}
public void post() {
// http:localhost:9090/api/server/user/{userId}/name/{userName}
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("api/server/user/{userId}/name/{userName}")
.encode()
.build()
.expand(100, "steve") // 순서대로 괄호랑 매치가 된다, ","로 구분하여 작성
.toUri();
System.out.println(uri);
//http body -> object -> object mapper -> json -> rest template -> http body json
UserRequest req = new UserRequest();
req.setName("steve");
req.setAge(10);
RestTemplate restTemplate = new RestTemplate();
// (uri, request object, response object)
ResponseEntity<String> response = restTemplate.postForEntity(uri, req, String.class);
System.out.println(response.getStatusCode());
System.out.println(response.getHeaders());
System.out.println(response.getBody());
//return response.getBody();
}
}
package com.example.client.service;
import com.example.client.dto.UserRequest;
import com.example.client.dto.UserResponse;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Service
public class RestTemplateService {
// http://localhost/api/server/hello
// response
public UserResponse hello() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/server/hello")
.queryParam("name","client")
.queryParam("age", 25)
.encode()
.build()
.toUri();
System.out.println(uri.toString());
RestTemplate restTemplate = new RestTemplate();
// getForObject 실행되는 순간이 클라이언트에서 http에서 서버로 붙는 시간
// String result = restTemplate.getForObject(uri, String.class);
ResponseEntity<UserResponse> result = restTemplate.getForEntity(uri, UserResponse.class);
System.out.println(result.getStatusCode());
System.out.println(result.getBody());
return result.getBody();
}
public void post() {
// http:localhost:9090/api/server/user/{userId}/name/{userName}
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("api/server/user/{userId}/name/{userName}")
.encode()
.build()
.expand(100, "steve") // 순서대로 괄호랑 매치가 된다, ","로 구분하여 작성
.toUri();
System.out.println(uri);
//http body -> object -> object mapper -> json -> rest template -> http body json
UserRequest req = new UserRequest();
req.setName("steve");
req.setAge(10);
RestTemplate restTemplate = new RestTemplate();
// (uri, request object, response object)
ResponseEntity<String> response = restTemplate.postForEntity(uri, req, String.class);
System.out.println(response.getStatusCode());
System.out.println(response.getHeaders());
System.out.println(response.getBody());
//return response.getBody();
}
public UserResponse exchange() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("api/server/user/{userId}/name/{userName}")
.encode()
.build()
.expand(100, "steve") // 순서대로 괄호랑 매치가 된다, ","로 구분하여 작성
.toUri();
System.out.println(uri);
//http body -> object -> object mapper -> json -> rest template -> http body json
UserRequest req = new UserRequest();
req.setName("steve");
req.setAge(10);
RequestEntity<UserRequest> requestRequestEntity = RequestEntity
.post(uri)
.contentType(MediaType.APPLICATION_JSON)
.header("x-authorization", "abcd")
.header("custom-header", "fffff")
.body(req);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<UserResponse> response = restTemplate.exchange(requestRequestEntity, UserResponse.class);
return response.getBody();
}
}
package com.example.client.controller;
import com.example.client.dto.UserResponse;
import com.example.client.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/client")
public class ApiController {
@Autowired
private RestTemplateService restTemplateService;
public ApiController(RestTemplateService restTemplateService) {
this.restTemplateService = restTemplateService;
}
@GetMapping("/hello")
public UserResponse getHello() {
restTemplateService.exchange();
return new UserResponse();
}
}
package com.example.server.contoller;
import com.example.server.dto.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/api/server")
public class ServerApiController {
@GetMapping("/hello")
public User hello(@RequestParam String name, @RequestParam int age) {
User user = new User();
user.setName(name);
user.setAge(age);
return user;
}
@PostMapping("/user/{userId}/name/{userName}")
public User post(
@RequestBody User user,
@PathVariable int userId,
@PathVariable String userName,
@RequestHeader("x-authorization") String authorization,
@RequestHeader("custom-header") String customHeader
) {
log.info("userId : {}, userName : {}", userId, userName);
log.info("authorization : {}", authorization);
log.info("customHeader : {}",customHeader);
log.info("client request : {}",user);
return user;
}
}
{
"name" : "steve",
"age" : 10
}
{
"header" : {
...
},
"body" : {
...
}
}
{
"header" : {
"response_code" : "OK"
},
"body" : {
"name" : "spring boot",
"age" : 1024
}
}
package com.example.client.dto;
public class Req<T> {
private Header header;
private T responseBody;
public static class Header {
private String responseCode;
public String getResponseCode() {
return responseCode;
}
public void setResponseCode(String responseCode) {
this.responseCode = responseCode;
}
@Override
public String toString() {
return "Header{" +
"responseCode='" + responseCode + '\'' +
'}';
}
}
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
public T getResponseBody() {
return responseBody;
}
public void setResponseBody(T responseBody) {
this.responseBody = responseBody;
}
@Override
public String toString() {
return "Req{" +
"header=" + header +
", body=" + responseBody +
'}';
}
}
package com.example.client.service;
import com.example.client.dto.Req;
import com.example.client.dto.UserRequest;
import com.example.client.dto.UserResponse;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Service
public class RestTemplateService {
// http://localhost/api/server/hello
// response
public UserResponse hello() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/server/hello")
.queryParam("name","client")
.queryParam("age", 25)
.encode()
.build()
.toUri();
System.out.println(uri.toString());
RestTemplate restTemplate = new RestTemplate();
// getForObject 실행되는 순간이 클라이언트에서 http에서 서버로 붙는 시간
// String result = restTemplate.getForObject(uri, String.class);
ResponseEntity<UserResponse> result = restTemplate.getForEntity(uri, UserResponse.class);
System.out.println(result.getStatusCode());
System.out.println(result.getBody());
return result.getBody();
}
public void post() {
// http:localhost:9090/api/server/user/{userId}/name/{userName}
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("api/server/user/{userId}/name/{userName}")
.encode()
.build()
.expand(100, "steve") // 순서대로 괄호랑 매치가 된다, ","로 구분하여 작성
.toUri();
System.out.println(uri);
//http body -> object -> object mapper -> json -> rest template -> http body json
UserRequest req = new UserRequest();
req.setName("steve");
req.setAge(10);
RestTemplate restTemplate = new RestTemplate();
// (uri, request object, response object)
ResponseEntity<String> response = restTemplate.postForEntity(uri, req, String.class);
System.out.println(response.getStatusCode());
System.out.println(response.getHeaders());
System.out.println(response.getBody());
//return response.getBody();
}
public UserResponse exchange() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("api/server/user/{userId}/name/{userName}")
.encode()
.build()
.expand(100, "steve") // 순서대로 괄호랑 매치가 된다, ","로 구분하여 작성
.toUri();
System.out.println(uri);
//http body -> object -> object mapper -> json -> rest template -> http body json
UserRequest req = new UserRequest();
req.setName("steve");
req.setAge(10);
RequestEntity<UserRequest> requestRequestEntity = RequestEntity
.post(uri)
.contentType(MediaType.APPLICATION_JSON)
.header("x-authorization", "abcd")
.header("custom-header", "fffff")
.body(req);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<UserResponse> response = restTemplate.exchange(requestRequestEntity, UserResponse.class);
return response.getBody();
}
public Req<UserResponse> genericExchange() {
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("api/server/user/{userId}/name/{userName}")
.encode()
.build()
.expand(100, "steve") // 순서대로 괄호랑 매치가 된다, ","로 구분하여 작성
.toUri();
System.out.println(uri);
//http body -> object -> object mapper -> json -> rest template -> http body json
UserRequest userRequest = new UserRequest();
userRequest.setName("steve");
userRequest.setAge(10);
Req req = new Req<UserRequest>();
req.setHeader(
new Req.Header()
);
req.setResponseBody(
userRequest
);
RequestEntity<Req<UserRequest>> requestEntity = RequestEntity
.post(uri)
.contentType(MediaType.APPLICATION_JSON)
.header("x-authorization", "abcd")
.header("custom-header", "fffff")
.body(req);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Req<UserResponse>> response
= restTemplate.exchange(requestEntity, new ParameterizedTypeReference<Req<UserResponse>>(){});
// Req<UserResponse>.class generic에는 .class를 붙일 수 없다
// 그렇기 때문에 ParameterizedTypeReference로 랩핑 해준다
return response.getBody();
}
}
package com.example.client.controller;
import com.example.client.dto.Req;
import com.example.client.dto.UserResponse;
import com.example.client.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/client")
public class ApiController {
@Autowired
private RestTemplateService restTemplateService;
public ApiController(RestTemplateService restTemplateService) {
this.restTemplateService = restTemplateService;
}
@GetMapping("/hello")
public Req<UserResponse> getHello() {
return restTemplateService.genericExchange();
}
}
package com.example.server.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Req<T> {
private Header header;
private T responseBody;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Header{
private String responseCode;
}
}
package com.example.server.contoller;
import com.example.server.dto.Req;
import com.example.server.dto.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/api/server")
public class ServerApiController {
@GetMapping("/hello")
public User hello(@RequestParam String name, @RequestParam int age) {
User user = new User();
user.setName(name);
user.setAge(age);
return user;
}
@PostMapping("/user/{userId}/name/{userName}")
public Req<User> post(
//HttpEntity<String> entity,
@RequestBody Req<User> user,
@PathVariable int userId,
@PathVariable String userName,
@RequestHeader("x-authorization") String authorization,
@RequestHeader("custom-header") String customHeader
) {
//log.info("req : {}", entity.getBody());
log.info("userId : {}, userName : {}", userId, userName);
log.info("authorization : {}", authorization);
log.info("customHeader : {}",customHeader);
//log.info("client request : {}",user);
Req<User> response = new Req<>();
response.setHeader(
new Req.Header()
);
response.setResponseBody(user.getResponseBody());
return response;
}
}