Data Conversion for Spring REST API
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import com.javadevjournal.rest.facade.OrderFacade;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class RestApiDataConversionApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RestApiDataConversionApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ModelMapper modelMapper(){
|
||||
return new ModelMapper();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OrderFacade orderFacade(){
|
||||
return new OrderFacade();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.javadevjournal.rest;
|
||||
|
||||
import com.javadevjournal.rest.data.Order;
|
||||
import com.javadevjournal.rest.dto.OrderDto;
|
||||
import com.javadevjournal.rest.facade.OrderFacade;
|
||||
import com.javadevjournal.rest.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class OrderController {
|
||||
|
||||
@Autowired
|
||||
private OrderFacade orderFacade;
|
||||
|
||||
@GetMapping(value = "/orders/{id}")
|
||||
public @ResponseBody ResponseEntity<OrderDto> getOrder(@PathVariable("id") String id){
|
||||
OrderDto order = orderFacade.getOrderById(id);
|
||||
return new ResponseEntity<>(order, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.javadevjournal.rest.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface JsonProperty {
|
||||
public String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.javadevjournal.rest.data;
|
||||
|
||||
public class Address {
|
||||
|
||||
private String addressLine1;
|
||||
private String street;
|
||||
private String city;
|
||||
private String postalCode;
|
||||
|
||||
public String getAddressLine1() {
|
||||
return addressLine1;
|
||||
}
|
||||
|
||||
public void setAddressLine1(String addressLine1) {
|
||||
this.addressLine1 = addressLine1;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return postalCode;
|
||||
}
|
||||
|
||||
public void setPostalCode(String postalCode) {
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.javadevjournal.rest.data;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonSetter;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
public class CustomHealth {
|
||||
|
||||
private Map<String, Object> healthDetails;
|
||||
|
||||
@JsonAnyGetter
|
||||
public Map<String, Object> getHealthDetails() {
|
||||
return this.healthDetails;
|
||||
}
|
||||
|
||||
@JsonSetter
|
||||
public void setHealthDetails(Map<String, Object> healthDetails) {
|
||||
this.healthDetails = healthDetails;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.javadevjournal.rest.data;
|
||||
|
||||
import com.javadevjournal.rest.annotation.JsonProperty;
|
||||
|
||||
|
||||
public class Customer {
|
||||
|
||||
@JsonProperty("userId")
|
||||
private int id;
|
||||
|
||||
@JsonProperty("customerName")
|
||||
private String name;
|
||||
|
||||
public Customer(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.javadevjournal.rest.data;
|
||||
|
||||
public class CustomerModel {
|
||||
|
||||
private String userId;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String email;
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.javadevjournal.rest.data;
|
||||
|
||||
public class Order {
|
||||
|
||||
private String orderNumber;
|
||||
private double orderAmount;
|
||||
private double tax;
|
||||
private CustomerModel customer;
|
||||
private Address shippingAddress;
|
||||
|
||||
public String getOrderNumber() {
|
||||
return orderNumber;
|
||||
}
|
||||
|
||||
public void setOrderNumber(String orderNumber) {
|
||||
this.orderNumber = orderNumber;
|
||||
}
|
||||
|
||||
public double getOrderAmount() {
|
||||
return orderAmount;
|
||||
}
|
||||
|
||||
public void setOrderAmount(double orderAmount) {
|
||||
this.orderAmount = orderAmount;
|
||||
}
|
||||
|
||||
public double getTax() {
|
||||
return tax;
|
||||
}
|
||||
|
||||
public void setTax(double tax) {
|
||||
this.tax = tax;
|
||||
}
|
||||
|
||||
public CustomerModel getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
|
||||
public void setCustomer(CustomerModel customer) {
|
||||
this.customer = customer;
|
||||
}
|
||||
|
||||
public Address getShippingAddress() {
|
||||
return shippingAddress;
|
||||
}
|
||||
|
||||
public void setShippingAddress(Address shippingAddress) {
|
||||
this.shippingAddress = shippingAddress;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.javadevjournal.rest.dto;
|
||||
|
||||
import com.javadevjournal.rest.data.Address;
|
||||
import com.javadevjournal.rest.data.CustomerModel;
|
||||
|
||||
public class OrderDto {
|
||||
|
||||
private String orderNumber;
|
||||
private double orderAmount;
|
||||
private double tax;
|
||||
private CustomerModel customer;
|
||||
private Address shippingAddress;
|
||||
|
||||
public String getOrderNumber() {
|
||||
return orderNumber;
|
||||
}
|
||||
|
||||
public void setOrderNumber(String orderNumber) {
|
||||
this.orderNumber = orderNumber;
|
||||
}
|
||||
|
||||
public double getOrderAmount() {
|
||||
return orderAmount;
|
||||
}
|
||||
|
||||
public void setOrderAmount(double orderAmount) {
|
||||
this.orderAmount = orderAmount;
|
||||
}
|
||||
|
||||
public double getTax() {
|
||||
return tax;
|
||||
}
|
||||
|
||||
public void setTax(double tax) {
|
||||
this.tax = tax;
|
||||
}
|
||||
|
||||
public CustomerModel getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
|
||||
public void setCustomer(CustomerModel customer) {
|
||||
this.customer = customer;
|
||||
}
|
||||
|
||||
public Address getShippingAddress() {
|
||||
return shippingAddress;
|
||||
}
|
||||
|
||||
public void setShippingAddress(Address shippingAddress) {
|
||||
this.shippingAddress = shippingAddress;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.javadevjournal.rest.facade;
|
||||
|
||||
import com.javadevjournal.rest.data.Order;
|
||||
import com.javadevjournal.rest.dto.OrderDto;
|
||||
import com.javadevjournal.rest.service.OrderService;
|
||||
import com.javadevjournal.rest.service.UserService;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
public class OrderFacade {
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@Qualifier("userServiceImpl")
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private ModelMapper modelMapper;
|
||||
|
||||
public OrderDto getOrderById(String id){
|
||||
userService.enableUser();
|
||||
return convertToOrderDto(orderService.getOrderById(id));
|
||||
}
|
||||
|
||||
private OrderDto convertToOrderDto(Order order) {
|
||||
OrderDto orderDto = modelMapper.map(order, OrderDto.class);
|
||||
return orderDto;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.javadevjournal.rest.service;
|
||||
|
||||
import com.javadevjournal.rest.data.Address;
|
||||
import com.javadevjournal.rest.data.CustomerModel;
|
||||
import com.javadevjournal.rest.data.Order;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("orderService")
|
||||
public class OrderService {
|
||||
|
||||
public Order getOrderById(String id){
|
||||
|
||||
Order order = new Order();
|
||||
order.setOrderNumber("1234");
|
||||
order.setOrderAmount(200.34);
|
||||
order.setTax(10.12);
|
||||
|
||||
//customer
|
||||
CustomerModel customer= new CustomerModel();
|
||||
customer.setUserId("umeshawasthi@javadevjournal.com");
|
||||
customer.setFirstName("Umesh");
|
||||
customer.setLastName("Awasthi");
|
||||
customer.setEmail("umeshawasthi@javadevjournal.com");
|
||||
order.setCustomer(customer);
|
||||
|
||||
Address shippingAddress = new Address();
|
||||
shippingAddress.setAddressLine1("Adress line 1");
|
||||
shippingAddress.setStreet("street 1");
|
||||
shippingAddress.setCity("San Jose");
|
||||
shippingAddress.setPostalCode("95111");
|
||||
|
||||
order.setShippingAddress(shippingAddress);
|
||||
|
||||
return order;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.javadevjournal.rest.service;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
public void enableUser();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.javadevjournal.rest.service;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("userServiceImpl")
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public void enableUser() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
|
||||
import com.javadevjournal.rest.data.Address;
|
||||
import com.javadevjournal.rest.data.CustomerModel;
|
||||
import com.javadevjournal.rest.data.Order;
|
||||
import com.javadevjournal.rest.dto.OrderDto;
|
||||
import org.junit.Test;
|
||||
import org.modelmapper.ModelMapper;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class OrderDataConversionUnitTest {
|
||||
|
||||
private ModelMapper mapper = new ModelMapper();
|
||||
|
||||
@Test
|
||||
public void convert_Order_To_OrderDto(){
|
||||
|
||||
Order order = new Order();
|
||||
order.setOrderNumber("1234");
|
||||
order.setOrderAmount(200.34);
|
||||
order.setTax(10.12);
|
||||
|
||||
//customer
|
||||
CustomerModel customer= new CustomerModel();
|
||||
customer.setUserId("umeshawasthi@javadevjournal.com");
|
||||
customer.setFirstName("Umesh");
|
||||
customer.setLastName("Awasthi");
|
||||
customer.setEmail("umeshawasthi@javadevjournal.com");
|
||||
order.setCustomer(customer);
|
||||
|
||||
Address shippingAddress = new Address();
|
||||
shippingAddress.setAddressLine1("Adress line 1");
|
||||
shippingAddress.setStreet("street 1");
|
||||
shippingAddress.setCity("San Jose");
|
||||
shippingAddress.setPostalCode("95111");
|
||||
|
||||
order.setShippingAddress(shippingAddress);
|
||||
|
||||
OrderDto orderDTO = mapper.map(order, OrderDto.class);
|
||||
|
||||
assertEquals(order.getCustomer().getFirstName(), orderDTO.getCustomer().getFirstName());
|
||||
assertEquals(order.getCustomer().getLastName(), orderDTO.getCustomer().getLastName());
|
||||
assertEquals(order.getOrderNumber(), orderDTO.getOrderNumber());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class RestApiDataConversionApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user