spring cloud : communicate with each service - rest template
This commit is contained in:
@@ -15,7 +15,7 @@ public class OrderEntity implements Serializable {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, length = 120, unique = true)
|
||||
@Column(nullable = false, length = 120)
|
||||
private String productId;
|
||||
|
||||
@Column(nullable = false)
|
||||
|
||||
@@ -3,8 +3,10 @@ package com.example.userservice;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@@ -18,4 +20,10 @@ public class UserServiceApplication {
|
||||
public BCryptPasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,20 @@ package com.example.userservice.service;
|
||||
import com.example.userservice.dto.UserDto;
|
||||
import com.example.userservice.entity.UserEntity;
|
||||
import com.example.userservice.repository.UserRepository;
|
||||
import com.example.userservice.vo.ResponseOrder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -22,6 +28,9 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final BCryptPasswordEncoder passwordEncoder;
|
||||
private final Environment env;
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
private final ModelMapper mapper = new ModelMapper();
|
||||
|
||||
@Override
|
||||
@@ -41,8 +50,15 @@ public class UserServiceImpl implements UserService {
|
||||
.orElseThrow(() -> new UsernameNotFoundException("user not found"));
|
||||
|
||||
UserDto userDto = mapper.map(userEntity, UserDto.class);
|
||||
userDto.setOrders(new ArrayList<>());
|
||||
// userDto.setOrders(new ArrayList<>());
|
||||
|
||||
String orderUrl = String.format(env.getProperty("order_service.url"), userId);
|
||||
ResponseEntity<List<ResponseOrder>> orderListResponse =
|
||||
restTemplate.exchange(orderUrl, HttpMethod.GET, null,
|
||||
new ParameterizedTypeReference<List<ResponseOrder>>() {
|
||||
});
|
||||
List<ResponseOrder> orderList = orderListResponse.getBody();
|
||||
userDto.setOrders(orderList);
|
||||
return userDto;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user