Commit 72577942 by jorgecacs

Initial commit

parent ba4b5f58
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>demo_itau</name>
<name>demo_api</name>
<comment></comment>
<projects>
</projects>
......
......@@ -4,7 +4,7 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<property name="context-root" value="demo_itau"/>
<property name="java-output-path" value="/demo_itau/target/classes"/>
<property name="context-root" value="demo_api"/>
</wb-module>
</project-modules>
......@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.roshka</groupId>
<artifactId>demo_itau</artifactId>
<artifactId>demo_api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>DemoRshk</name>
......@@ -23,8 +23,44 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
<version>2.9.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.20</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -2,13 +2,14 @@ package com.rshk.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
public class AppConfiguration {
@PropertySource("classpath:application.properties")
public class AppConfiguration extends AnnotationConfigWebApplicationContext{
@Value(value = "variable")
private String variable;
}
......@@ -13,14 +13,17 @@ public class Customer {
@JsonProperty(value = "customer_id", required = true)
private int id;
@JsonProperty(value = "customer_name", required = true)
private String name;
@JsonProperty(value = "customer_address", required = true)
private String address;
@JsonProperty(value = "customer_number", required = true)
private String phoneNumber;
@JsonProperty(value = "customer_document", required = true)
private String document;
}
package com.rshk.demo.beans;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class SaveCustomerRespData {
@JsonProperty(value = "data")
private int data;
}
......@@ -7,6 +7,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
......@@ -17,7 +18,7 @@ import com.rshk.demo.beans.CustomerListData;
import com.rshk.demo.services.CustomerService;
@RestController
@RequestMapping("/customers")
@RequestMapping("/api-customers")
public class CustomerController {
@Autowired
......@@ -29,9 +30,7 @@ public class CustomerController {
try {
ResponseEntity response = null;
List<Customer> customerList = customerService.getCustomers();
return new ResponseEntity(new CustomerListData(customerList), HttpStatus.OK);
return new ResponseEntity(customerService.getCustomers(), HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
......@@ -45,12 +44,23 @@ public class CustomerController {
public ResponseEntity get(@PathVariable String id) {
try {
ResponseEntity response = null;
Customer customer = customerService.get(id);
return new ResponseEntity(new CustomerGetRespData(customer), HttpStatus.OK);
}catch(Exception e) {
return new ResponseEntity(customerService.get(id), HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<Error>(new Error(), HttpStatus.UNPROCESSABLE_ENTITY);
}
}
@RequestMapping(value = "/save", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity get(@RequestBody Customer customer) {
try {
ResponseEntity response = null;
return new ResponseEntity(customerService.insert(customer), HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<Error>(new Error(), HttpStatus.UNPROCESSABLE_ENTITY);
}
......
......@@ -3,13 +3,16 @@ package com.rshk.demo.services;
import java.util.List;
import com.rshk.demo.beans.Customer;
import com.rshk.demo.beans.CustomerGetRespData;
import com.rshk.demo.beans.CustomerListData;
import com.rshk.demo.beans.SaveCustomerRespData;
public interface CustomerService {
void insert(Customer customer);
Customer get(String document);
List<Customer> getCustomers();
SaveCustomerRespData insert(Customer customer) throws Exception;
CustomerGetRespData get(String document) throws Exception;
CustomerListData getCustomers() throws Exception;
}
package com.rshk.demo.services;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rshk.demo.beans.Customer;
import com.rshk.demo.beans.CustomerGetRespData;
import com.rshk.demo.beans.CustomerListData;
import com.rshk.demo.beans.SaveCustomerRespData;
@Service
public class CustomerServiceImpl implements CustomerService {
public void insert(Customer customer) {
// TODO Auto-generated method stub
@Value("${customer_mcs}")
private String mcsUrl;
private final static Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);
public SaveCustomerRespData insert(Customer customer) throws Exception {
try {
HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(mcsUrl.concat("/save"));
logger.info("We're going to call -> " + request.getURI().toString());
request.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(customer)));
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
logger.info("Request -> " + request);
HttpResponse response = client.execute(request);
String responseString = EntityUtils.toString(response.getEntity());
logger.info("Getting response -> " + responseString);
SaveCustomerRespData resp = new ObjectMapper().readValue(responseString, SaveCustomerRespData.class);
return resp;
} catch (Exception e) {
logger.error("Error while trying to get customers ->", e);
e.printStackTrace();
throw e;
}
}
public Customer get(String document) {
// TODO Auto-generated method stub
return null;
public CustomerGetRespData get(String document) throws Exception {
try {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(mcsUrl.concat(String.format("/%s", document)));
logger.info("We're going to call -> " + request.getURI().toString());
HttpResponse response = client.execute(request);
String responseString = EntityUtils.toString(response.getEntity());
logger.info("Getting response -> " + responseString);
/// deserialize response body into an object
CustomerGetRespData data = new ObjectMapper().readValue(responseString, CustomerGetRespData.class);
return data;
} catch (Exception e) {
logger.error("Error while trying to get customer ->", e);
e.printStackTrace();
throw e;
}
}
public List<Customer> getCustomers() {
// TODO Auto-generated method stub
return null;
public CustomerListData getCustomers() throws Exception {
try {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(mcsUrl.concat("/list"));
logger.info("We're going to call -> " + request.getURI().toString());
HttpResponse response = client.execute(request);
String responseString = EntityUtils.toString(response.getEntity());
logger.info("Getting response -> " + responseString);
CustomerListData data = new ObjectMapper().readValue(responseString, CustomerListData.class);
return data;
} catch (Exception e) {
logger.error("Error while trying to get customers ->", e);
e.printStackTrace();
throw e;
}
}
}
customer_mcs=http://localhost:8080/demo_mcs/customer
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<mvc:annotation-driven>
</mvc:annotation-driven>
<context:component-scan
base-package="com.rshk.demo" />
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>demo_itau</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<display-name>demo_api</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="com.memorynotfound" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
\ No newline at end of file
customer_mcs=http://localhost:8080/demo_mcs/customer
\ No newline at end of file
#Generated by Maven Integration for Eclipse
#Tue Jan 14 21:44:08 PYST 2020
#Fri Jan 17 07:51:56 PYST 2020
version=0.0.1-SNAPSHOT
groupId=com.demo.roshka
m2e.projectName=demo_itau
m2e.projectLocation=/Users/jorgecaceresflores/Documents/workspace-sts-3.9.11.RELEASE/demo_itau
artifactId=demo_itau
m2e.projectName=demo_api
m2e.projectLocation=/Users/jorgecaceresflores/Documents/workspace-sts-3.9.11.RELEASE/demo_api
artifactId=demo_api
......@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.roshka</groupId>
<artifactId>demo_itau</artifactId>
<artifactId>demo_api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>DemoRshk</name>
......@@ -23,8 +23,44 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
<version>2.9.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.20</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="com.memorynotfound" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment