Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json api and web service solution #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions json-api-and-web-service-solution/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Hi,


## Implementation plan

* Create rest api aplication
* Add JQuery for send/get data in JSON format
* Implement expression calculation
* Catch all possible exceptions
* Integrate with Travis CI
* Make some refac/add test/finally review

## User guide
73 changes: 73 additions & 0 deletions json-api-and-web-service-solution/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>json.api.and.web.service.solution</groupId>
<artifactId>JsonApiAndWebServiceSolution</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<spring.version>4.3.10.RELEASE</spring.version>
</properties>

<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>

</dependencies>


<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>JsonApiAndWebServiceSolution</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package job.interview.exercises.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "job.interview.exercises")
public class AppConfig extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");

return viewResolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package job.interview.exercises.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInit extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}

@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package job.interview.exercises.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import job.interview.exercises.service.AppService;

@Controller
public class AppController {

@Autowired
@Qualifier("AppService")
AppService appService;

@RequestMapping(value = "/")
public ModelAndView main() {

final ModelAndView mav = new ModelAndView("main");
mav.addObject("test", appService.getMsg());

return mav;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package job.interview.exercises.controller.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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;

import job.interview.exercises.model.Result;
import job.interview.exercises.service.AppService;

@RestController
@RequestMapping(value = "/rest")
public class AppRestController {

@Autowired
@Qualifier("AppService")
AppService appService;

@RequestMapping(value = "/calculateExpression", method = RequestMethod.POST, produces = "application/json")
public Result getTestInJSON(@RequestBody String expression) {

final Result calculatedExpression = appService.calculateExpression(expression);
return calculatedExpression;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package job.interview.exercises.model;

import java.io.Serializable;

public class Result implements Serializable {

private static final long serialVersionUID = -4902676973289000860L;

private String result;

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

@Override
public String toString() {
return "Result [result=" + result + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package job.interview.exercises.service;

import job.interview.exercises.model.Result;

public interface AppService {

String getMsg();

Result calculateExpression(String expression);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package job.interview.exercises.service;

import org.springframework.stereotype.Service;

import job.interview.exercises.model.Result;

@Service("AppService")
public class AppServiceImpl implements AppService {

@Override
public String getMsg() {
return "Test msg";
}

@Override
public Result calculateExpression(String expression) {
final Result result = new Result();
result.setResult(expression + " test");

return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html lang="en">
<head>

<c:url var="home" value="/" scope="request" />
<spring:url value="resources/css/style.css" var="coreCss" />
<spring:url value="resources/css/bootstrap.min.css" var="bootstrapCss" />

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="JSON API WS">
<meta name="author" content="Alex">

<title>JSON API WS</title>

<link href="${bootstrapCss}" rel="stylesheet">
<link href="${coreCss}" rel="stylesheet">
</head>

<body>

<div class="container">

<div class="central-block">

<div id="feedback"></div>

<form class="form-horizontal" id="search-form">
<div class="form-group form-group-lg">
<label class="col-sm-2 control-label">Expression</label>
<div class="col-sm-10">
<input type=text class="form-control" id="expression">
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="bth-search"
class="btn btn-primary btn-lg">Calculate</button>
</div>
</div>
</form>
</div>

</div>

<script src="resources/js/jquery-3.2.1.min.js"></script>
<script src="resources/js/bootstrap.min.js"></script>
<script src="resources/js/script.js"></script>

<script>
jQuery(document).ready(function($) {

$("#search-form").submit(function(event) {
enableSearchButton(false);
event.preventDefault();
searchViaAjax();
});

});

function searchViaAjax() {
var expression = $("#expression").val();

$.ajax({
type : "POST",
contentType : "application/json",
url : "/JsonApiAndWebServiceSolution/rest/calculateExpression",
data: expression,
dataType : 'json',
timeout : 100000,
success : function(data) {
console.log("SUCCESS: ", data);
display(data);
},
error : function(e) {
console.log("ERROR: ", e);
display(e);
},
done : function(e) {
console.log("DONE");
enableSearchButton(true);
}
});

}

function enableSearchButton(flag) {
$("#btn-search").prop("disabled", flag);
}

function display(data) {
var json = "<h4>Response</h4><pre>" + JSON.stringify(data, null, 4) + "</pre>";
$('#feedback').html(json);
}
</script>

</body>
</html>

Large diffs are not rendered by default.

Loading