주문시 상품 변경 추가
This commit is contained in:
@@ -2,9 +2,15 @@ package com.example.template;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@@ -28,13 +34,33 @@ public class Order {
|
||||
@PostPersist
|
||||
private void publishOrderPlaced() {
|
||||
KafkaTemplate kafkaTemplate = Application.applicationContext.getBean(KafkaTemplate.class);
|
||||
RestTemplate restTemplate = Application.applicationContext.getBean(RestTemplate.class);
|
||||
|
||||
Environment env = Application.applicationContext.getEnvironment();
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String json = null;
|
||||
|
||||
if( productId == null ){
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
String productUrl = env.getProperty("productUrl") + "/products/" + productId;
|
||||
|
||||
// 1. checkInventory
|
||||
ResponseEntity<String> productEntity = restTemplate.getForEntity(productUrl, String.class);
|
||||
System.out.println(productEntity.getStatusCode());
|
||||
System.out.println(productEntity.getBody());
|
||||
|
||||
JsonParser parser = new JsonParser();
|
||||
JsonObject jsonObject = parser.parse(productEntity.getBody()).getAsJsonObject();
|
||||
|
||||
OrderPlaced orderPlaced = new OrderPlaced();
|
||||
try {
|
||||
orderPlaced.setOrderId(id);
|
||||
|
||||
this.setPrice(jsonObject.get("price").getAsInt());
|
||||
this.setProductName(jsonObject.get("name").getAsString());
|
||||
|
||||
BeanUtils.copyProperties(this, orderPlaced);
|
||||
json = objectMapper.writeValueAsString(orderPlaced);
|
||||
} catch (JsonProcessingException e) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.template.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
RestTemplate restTemplate() {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||
converter.setObjectMapper(new ObjectMapper());
|
||||
restTemplate.getMessageConverters().add(converter);
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,9 @@ logging:
|
||||
org:
|
||||
hibernate:
|
||||
type: trace
|
||||
|
||||
productUrl: http://localhost:8085
|
||||
|
||||
server:
|
||||
port: 8081
|
||||
---
|
||||
@@ -32,3 +35,5 @@ spring:
|
||||
bootstrap-servers: my-kafka.kafka.svc.cluster.local:9092
|
||||
consumer:
|
||||
enable-auto-commit: true
|
||||
|
||||
productUrl: http://product:8085
|
||||
Reference in New Issue
Block a user