diff --git a/gson-jackson-performance/README.md b/gson-jackson-performance/README.md index 9bbe679dfc..5b08f6cdec 100644 --- a/gson-jackson-performance/README.md +++ b/gson-jackson-performance/README.md @@ -1,5 +1,3 @@ -## Performance of GSON and Jackson +## Performance of Gson and Jackson -## STandalone java programs to measure the performance of both JSON API's - -## based on file size , iterations. \ No newline at end of file +Standalone java programs to measure the performance of both JSON APIs based on file size and object graph complexity. diff --git a/json-path/pom.xml b/json-path/pom.xml index ca37447a50..c03385cf1d 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -14,13 +14,6 @@ ${json-path.version} - - - joda-time - joda-time - ${joda-time.version} - - junit @@ -44,13 +37,24 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + 2.1.0 - - 2.9.2 - 4.12 diff --git a/json-path/src/main/resources/intro_service.json b/json-path/src/main/resources/intro_service.json new file mode 100644 index 0000000000..448492463a --- /dev/null +++ b/json-path/src/main/resources/intro_service.json @@ -0,0 +1,61 @@ +[ + { + "id": 1, + "title": "Casino Royale", + "director": "Martin Campbell", + "starring": + [ + "Daniel Craig", + "Eva Green" + ], + + "desc": "Twenty-first James Bond movie", + "release date": 1163466000000, + "box office": 594275385 + }, + + { + "id": 2, + "title": "Quantum of Solace", + "director": "Marc Forster", + "starring": + [ + "Daniel Craig", + "Olga Kurylenko" + ], + + "desc": "Twenty-second James Bond movie", + "release date": 1225242000000, + "box office": 591692078 + }, + + { + "id": 3, + "title": "Skyfall", + "director": "Sam Mendes", + "starring": + [ + "Daniel Craig", + "Naomie Harris" + ], + + "desc": "Twenty-third James Bond movie", + "release date": 1350954000000, + "box office": 1110526981 + }, + + { + "id": 4, + "title": "Spectre", + "director": "Sam Mendes", + "starring": + [ + "Daniel Craig", + "Lea Seydoux" + ], + + "desc": "Twenty-fourth James Bond movie", + "release date": 1445821200000, + "box office": 879376275 + } +] \ No newline at end of file diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/ChangingPasswordTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/ChangingPasswordTest.java deleted file mode 100644 index 9f616e1a32..0000000000 --- a/json-path/src/test/java/com/baeldung/jsonpath/introduction/ChangingPasswordTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.baeldung.jsonpath.introduction; - -import static org.junit.Assert.*; - -import org.junit.Test; - -import java.io.InputStream; -import java.util.Arrays; -import java.util.List; -import java.util.Scanner; - -import org.joda.time.DateTime; -import org.joda.time.Years; - -import com.jayway.jsonpath.Configuration; -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.Option; - -public class ChangingPasswordTest { - - enum Result { - SUCCESS, FAILURE - } - - InputStream jsonValueInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_user.json"); - String jsonDataSourceString = new Scanner(jsonValueInputStream, "UTF-8").useDelimiter("\\Z").next(); - - @Test - public void givenUseCase_whenChangingPasswordOfNonExistentUser_thenFail() { - String failedRequestBody = "{\"username\":\"jayway\", \"new_password\":\"JsonPath\"}"; - Result result = changingPasswordHelper(failedRequestBody); - - assertEquals(Result.FAILURE, result); - } - - @Test - public void givenUseCase_whenChangingToUnusedPassword_thenSucceed() { - String successfulRequestBody = "{\"username\":\"oracle\", \"new_password\":\"Java_SE_9\"}"; - Result result = changingPasswordHelper(successfulRequestBody); - - assertEquals(Result.SUCCESS, result); - } - - @Test - public void givenUseCase_whenChangingToRecentlyUsedPassword_thenFail() { - String failedRequestBody = "{\"username\":\"oracle\", \"new_password\":\"Java_SE_7\"}"; - Result result = changingPasswordHelper(failedRequestBody); - - assertEquals(Result.FAILURE, result); - } - - @Test - public void givenUseCase_whenChangingToLongTimeAgoPassword_thenSucceed() { - String successfulRequestBody = "{\"username\":\"sun\", \"new_password\":\"J2SE_5.0\"}"; - Result result = changingPasswordHelper(successfulRequestBody); - - assertEquals(Result.SUCCESS, result); - } - - private Result changingPasswordHelper(String requestBody) { - DocumentContext requestContext = JsonPath.parse(requestBody); - String extractedUsername = requestContext.read("$['username']"); - String extractedPassword = requestContext.read("$['new_password']"); - - DocumentContext jsonContext = JsonPath.parse(jsonDataSourceString); - List dataSourceUsername = jsonContext.read("$[?(@.username == '" + extractedUsername + "')]"); - if (dataSourceUsername.size() == 0) - return Result.FAILURE; - - Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build(); - List pathToCurrentUser = JsonPath.using(pathConfiguration).parse(jsonDataSourceString).read("$[?(@.username == '" + extractedUsername + "')]"); - List passwordCreatedTimeList = jsonContext.read(pathToCurrentUser.get(0) + "['password']['old'][?(@.value == '" + extractedPassword + "')]['created']"); - if (passwordCreatedTimeList.size() == 0) - return Result.SUCCESS; - - Long[] passwordCreatedTimeArray = (passwordCreatedTimeList.toArray(new Long[passwordCreatedTimeList.size()])); - Arrays.sort(passwordCreatedTimeArray); - DateTime oldDate = new DateTime(passwordCreatedTimeArray[passwordCreatedTimeArray.length - 1]); - if (Years.yearsBetween(oldDate, new DateTime()).getYears() <= 10) - return Result.FAILURE; - - return Result.SUCCESS; - } -} diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/LoggingInTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/LoggingInTest.java deleted file mode 100644 index 9f0be349c9..0000000000 --- a/json-path/src/test/java/com/baeldung/jsonpath/introduction/LoggingInTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.jsonpath.introduction; - -import static org.junit.Assert.*; - -import org.junit.Test; - -import java.io.InputStream; -import java.util.List; -import java.util.Scanner; - -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; - -public class LoggingInTest { - - enum Result { - SUCCESS, FAILURE - } - - InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_user.json"); - String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); - - @Test - public void givenUseCase_whenLoggingInWithCorrectUserData_thenSucceed() { - String correctRequestBody = "{\"username\":\"sun\", \"password\":\"Java_SE_6\"}"; - Result result = loggingInHelper(correctRequestBody); - - assertEquals(Result.SUCCESS, result); - } - - @Test - public void givenUseCase_whenLoggingInWithIncorrectUserData_thenFail() { - String incorrectRequestBody = "{\"username\":\"oracle\", \"password\":\"Java_SE_9\"}"; - Result result = loggingInHelper(incorrectRequestBody); - - assertEquals(Result.FAILURE, result); - } - - private Result loggingInHelper(String requestBody) { - DocumentContext requestContext = JsonPath.parse(requestBody); - String extractedUsername = requestContext.read("$['username']"); - String extractedPassword = requestContext.read("$['password']"); - List list = JsonPath.parse(jsonDataSourceString).read("$[?(@.username == '" + extractedUsername + "' && @.password.current.value == '" + extractedPassword + "')]"); - - if (list.size() == 0) - return Result.FAILURE; - return Result.SUCCESS; - } - -} diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/RegisteringAccountTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/RegisteringAccountTest.java deleted file mode 100644 index 04bb04528a..0000000000 --- a/json-path/src/test/java/com/baeldung/jsonpath/introduction/RegisteringAccountTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.jsonpath.introduction; - -import static org.junit.Assert.*; - -import org.junit.Test; - -import java.io.InputStream; -import java.util.List; -import java.util.Scanner; - -import com.jayway.jsonpath.JsonPath; - -public class RegisteringAccountTest { - - enum Result { - SUCCESS, FAILURE - } - - InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_user.json"); - String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); - - @Test - public void givenUseCase_whenRegisteringUnusedAccount_thenSucceed() { - String unusedRequestBody = "{\"username\":\"jayway\", \"password\":\"JsonPath\"}"; - Result result = registeringNewAccountHelper(unusedRequestBody); - - assertEquals(Result.SUCCESS, result); - } - - @Test - public void givenUseCase_whenRegisteringUsedAccount_thenFail() { - String usedRequestBody = "{\"username\":\"oracle\", \"password\":\"Java_SE_9\"}"; - Result result = registeringNewAccountHelper(usedRequestBody); - - assertEquals(Result.FAILURE, result); - } - - private Result registeringNewAccountHelper(String requestBody) { - List userDataSource = JsonPath.parse(jsonDataSourceString).read("$[*]['username']"); - String extractedUsername = JsonPath.parse(requestBody).read("$['username']"); - - if (userDataSource.toString().contains(extractedUsername)) - return Result.FAILURE; - return Result.SUCCESS; - } -} diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java new file mode 100644 index 0000000000..868acac8d0 --- /dev/null +++ b/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java @@ -0,0 +1,91 @@ +package com.baeldung.jsonpath.introduction; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.hamcrest.CoreMatchers.containsString; + +import org.junit.Test; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; + +public class ServiceTest { + InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_service.json"); + String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); + + @Test + public void givenId_whenRequestingRecordData_thenSucceed() { + Object dataObject = JsonPath.parse(jsonString).read("$[?(@.id == 2)]"); + String dataString = dataObject.toString(); + + assertThat(dataString, containsString("2")); + assertThat(dataString, containsString("Quantum of Solace")); + assertThat(dataString, containsString("Twenty-second James Bond movie")); + } + + @Test + public void givenStarring_whenRequestingMovieTitle_thenSucceed() { + List> dataList = JsonPath.parse(jsonString).read("$[?('Eva Green' in @['starring'])]"); + String title = (String) dataList.get(0).get("title"); + + assertEquals("Casino Royale", title); + } + + @Test + public void givenCompleteStructure_whenCalculatingTotalRevenue_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + int length = context.read("$.length()"); + long revenue = 0; + for (int i = 0; i < length; i++) { + revenue += context.read("$[" + i + "]['box office']", Long.class); + } + + assertEquals(594275385L + 591692078L + 1110526981L + 879376275L, revenue); + } + + @Test + public void givenStructure_whenRequestingHighestRevenueMovieTitle_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + List revenueList = context.read("$[*]['box office']"); + Integer[] revenueArray = revenueList.toArray(new Integer[0]); + Arrays.sort(revenueArray); + + int highestRevenue = revenueArray[revenueArray.length - 1]; + Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build(); + List pathList = JsonPath.using(pathConfiguration).parse(jsonString).read("$[?(@['box office'] == " + highestRevenue + ")]"); + + Map dataRecord = context.read(pathList.get(0)); + String title = dataRecord.get("title"); + + assertEquals("Skyfall", title); + } + + @Test + public void givenDirector_whenRequestingLatestMovieTitle_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + List> dataList = context.read("$[?(@.director == 'Sam Mendes')]"); + + List dateList = new ArrayList<>(); + for (Map item : dataList) { + Object date = item.get("release date"); + dateList.add(date); + } + Long[] dateArray = dateList.toArray(new Long[0]); + Arrays.sort(dateArray); + + long latestTime = dateArray[dateArray.length - 1]; + List> finalDataList = context.read("$[?(@['director'] == 'Sam Mendes' && @['release date'] == " + latestTime + ")]"); + String title = (String) finalDataList.get(0).get("title"); + + assertEquals("Spectre", title); + } +} \ No newline at end of file diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index 896d744e99..75efdca423 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung 0.1-SNAPSHOT spring-mvc-xml diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfig.java similarity index 93% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfig.java index c4d819caa5..b5238b04d5 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring; +package com.baeldung.spring; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java similarity index 97% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java index d2b57da818..06b2c0e461 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java @@ -1,4 +1,4 @@ -package org.baeldung.spring; +package com.baeldung.spring; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.ViewResolver; diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java similarity index 93% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java index 1dbe230adc..baffe7b503 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java @@ -1,8 +1,8 @@ -package org.baeldung.spring.controller; +package com.baeldung.spring.controller; import javax.validation.Valid; -import org.baeldung.spring.form.Employee; +import com.baeldung.spring.form.Employee; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java new file mode 100644 index 0000000000..39dabf86ed --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java @@ -0,0 +1,84 @@ +package com.baeldung.spring.controller; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.validation.Valid; + +import com.baeldung.spring.form.Person; +import com.baeldung.spring.validator.PersonValidator; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class PersonController { + + @Autowired + PersonValidator validator; + + @RequestMapping(value = "/person", method = RequestMethod.GET) + public ModelAndView showForm(final Model model) { + + initData(model); + return new ModelAndView("personForm", "person", new Person()); + } + + @RequestMapping(value = "/addPerson", method = RequestMethod.POST) + public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, + final ModelMap modelMap, final Model model) { + + validator.validate(person, result); + + if (result.hasErrors()) { + + initData(model); + return "personForm"; + } + + modelMap.addAttribute("person", person); + + return "personView"; + } + + private void initData(final Model model) { + + final List favouriteLanguageItem = new ArrayList(); + favouriteLanguageItem.add("Java"); + favouriteLanguageItem.add("C++"); + favouriteLanguageItem.add("Perl"); + model.addAttribute("favouriteLanguageItem", favouriteLanguageItem); + + final List jobItem = new ArrayList(); + jobItem.add("Full time"); + jobItem.add("Part time"); + model.addAttribute("jobItem", jobItem); + + final Map countryItems = new LinkedHashMap(); + countryItems.put("US", "United Stated"); + countryItems.put("IT", "Italy"); + countryItems.put("UK", "United Kingdom"); + countryItems.put("FR", "Grance"); + model.addAttribute("countryItems", countryItems); + + final List fruit = new ArrayList(); + fruit.add("Banana"); + fruit.add("Mango"); + fruit.add("Apple"); + model.addAttribute("fruit", fruit); + + final List books = new ArrayList(); + books.add("The Great Gatsby"); + books.add("Nineteen Eighty-Four"); + books.add("The Lord of the Rings"); + model.addAttribute("books", books); + } +} diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java similarity index 95% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java index 70132b9665..f41496212f 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.form; +package com.baeldung.spring.form; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java new file mode 100644 index 0000000000..88e4f9ff4c --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java @@ -0,0 +1,152 @@ +package com.baeldung.spring.form; + +import java.util.List; + +import org.hibernate.validator.constraints.NotEmpty; +import org.springframework.web.multipart.MultipartFile; + +public class Person { + + private long id; + + private String name; + private String email; + private String dateOfBirth; + + @NotEmpty + private String password; + private String sex; + private String country; + private String book; + private String job; + private boolean receiveNewsletter; + private String[] hobbies; + private List favouriteLanguage; + private List fruit; + private String notes; + private MultipartFile file; + + public Person() { + super(); + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(final String email) { + this.email = email; + } + + public String getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(final String dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + + public String getPassword() { + return password; + } + + public void setPassword(final String password) { + this.password = password; + } + + public String getSex() { + return sex; + } + + public void setSex(final String sex) { + this.sex = sex; + } + + public String getCountry() { + return country; + } + + public void setCountry(final String country) { + this.country = country; + } + + public String getJob() { + return job; + } + + public void setJob(final String job) { + this.job = job; + } + + public boolean isReceiveNewsletter() { + return receiveNewsletter; + } + + public void setReceiveNewsletter(final boolean receiveNewsletter) { + this.receiveNewsletter = receiveNewsletter; + } + + public String[] getHobbies() { + return hobbies; + } + + public void setHobbies(final String[] hobbies) { + this.hobbies = hobbies; + } + + public List getFavouriteLanguage() { + return favouriteLanguage; + } + + public void setFavouriteLanguage(final List favouriteLanguage) { + this.favouriteLanguage = favouriteLanguage; + } + + public String getNotes() { + return notes; + } + + public void setNotes(final String notes) { + this.notes = notes; + } + + public List getFruit() { + return fruit; + } + + public void setFruit(final List fruit) { + this.fruit = fruit; + } + + public String getBook() { + return book; + } + + public void setBook(final String book) { + this.book = book; + } + + public MultipartFile getFile() { + return file; + } + + public void setFile(final MultipartFile file) { + this.file = file; + } +} diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java new file mode 100644 index 0000000000..3a271f6545 --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java @@ -0,0 +1,22 @@ +package com.baeldung.spring.validator; + +import com.baeldung.spring.form.Person; +import org.springframework.stereotype.Component; +import org.springframework.validation.Errors; +import org.springframework.validation.ValidationUtils; +import org.springframework.validation.Validator; + +@Component +public class PersonValidator implements Validator { + + @Override + public boolean supports(final Class calzz) { + return Person.class.isAssignableFrom(calzz); + } + + @Override + public void validate(final Object obj, final Errors errors) { + + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name"); + } +} \ No newline at end of file diff --git a/spring-mvc-xml/src/main/resources/messages.properties b/spring-mvc-xml/src/main/resources/messages.properties new file mode 100644 index 0000000000..2a3cccf76c --- /dev/null +++ b/spring-mvc-xml/src/main/resources/messages.properties @@ -0,0 +1,2 @@ +required.name = Name is required! +NotEmpty.person.password = Password is required! \ No newline at end of file diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index a7aa252c08..e9e2f1c536 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -11,7 +11,9 @@ > - + + + @@ -19,4 +21,15 @@ + + + + + + + + + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp new file mode 100644 index 0000000000..a26fb6c3ea --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -0,0 +1,124 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + + Form Example - Register a Person + + + + + + +

Welcome, Enter The Person Details

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name
E-mail
Date of birth
Password
Sex + Male:
+ Female: +
Job + +
Country + +
Book + + + + +
Fruit + +
Receive newsletter
Hobbies + Bird watching: + Astronomy: + Snowboarding: +
Favourite languages + +
Notes
Select a file to upload
+ +
+ + + + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp new file mode 100644 index 0000000000..8893314d20 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp @@ -0,0 +1,80 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + Spring MVC Form Handling + + + +

Submitted Person Information

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Id :${person.id}
Name :${person.name}
Date of birth :${person.dateOfBirth}
Password :${person.password}
Sex :${person.sex}
Job :${person.job}
Country :${person.country}
Fruit :[]
Book :${person.book}
Receive Newsletter :${person.receiveNewsletter}
Hobbies :[]
Favourite Languages :[]
Notes :${person.notes}
+ +

Submitted File

+ + + + + + + + + + + + +
OriginalFileName :${person.file.originalFilename}
Type :${person.file.contentType}
+ + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 5275efdf24..aeaa9d394e 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -16,7 +16,7 @@ contextConfigLocation - org.baeldung.spring + com.baeldung.spring @@ -28,6 +28,9 @@ mvc org.springframework.web.servlet.DispatcherServlet 1 + + /tmp + mvc diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java index fec03d1ddb..89e3a22d45 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java @@ -17,13 +17,13 @@ public class ActuatorMetricService implements IActuatorMetricService { @Autowired private MetricReaderPublicMetrics publicMetrics; - private final List> statusMetric; + private final List> statusMetricsByMinute; private final List statusList; private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public ActuatorMetricService() { super(); - statusMetric = new ArrayList>(); + statusMetricsByMinute = new ArrayList>(); statusList = new ArrayList(); } @@ -31,7 +31,7 @@ public class ActuatorMetricService implements IActuatorMetricService { public Object[][] getGraphData() { final Date current = new Date(); final int colCount = statusList.size() + 1; - final int rowCount = statusMetric.size() + 1; + final int rowCount = statusMetricsByMinute.size() + 1; final Object[][] result = new Object[rowCount][colCount]; result[0][0] = "Time"; int j = 1; @@ -41,19 +41,23 @@ public class ActuatorMetricService implements IActuatorMetricService { j++; } - List temp; - List last = new ArrayList(); for (int i = 1; i < rowCount; i++) { - temp = statusMetric.get(i - 1); result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); - for (j = 1; j <= temp.size(); j++) { - result[i][j] = temp.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0); + } + + List minuteOfStatuses; + List last = new ArrayList(); + + for (int i = 1; i < rowCount; i++) { + minuteOfStatuses = statusMetricsByMinute.get(i - 1); + for (j = 1; j <= minuteOfStatuses.size(); j++) { + result[i][j] = minuteOfStatuses.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0); } while (j < colCount) { result[i][j] = 0; j++; } - last = temp; + last = minuteOfStatuses; } return result; } @@ -62,16 +66,16 @@ public class ActuatorMetricService implements IActuatorMetricService { @Scheduled(fixedDelay = 60000) private void exportMetrics() { - final ArrayList statusCount = initializeCounterList(statusList.size()); + final ArrayList lastMinuteStatuses = initializeStatuses(statusList.size()); for (final Metric counterMetric : publicMetrics.metrics()) { - updateStatusCount(counterMetric, statusCount); + updateMetrics(counterMetric, lastMinuteStatuses); } - statusMetric.add(statusCount); + statusMetricsByMinute.add(lastMinuteStatuses); } - private ArrayList initializeCounterList(final int size) { + private ArrayList initializeStatuses(final int size) { final ArrayList counterList = new ArrayList(); for (int i = 0; i < size; i++) { counterList.add(0); @@ -79,22 +83,26 @@ public class ActuatorMetricService implements IActuatorMetricService { return counterList; } - private void updateStatusCount(final Metric counterMetric, final ArrayList statusCount) { + private void updateMetrics(final Metric counterMetric, final ArrayList statusCount) { String status = ""; int index = -1; int oldCount = 0; if (counterMetric.getName().contains("counter.status.")) { status = counterMetric.getName().substring(15, 18); // example 404, 200 - if (!statusList.contains(status)) { - statusList.add(status); - statusCount.add(0); - } + appendStatusIfNotExist(status, statusCount); index = statusList.indexOf(status); oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index); statusCount.set(index, counterMetric.getValue().intValue() + oldCount); } } + private void appendStatusIfNotExist(final String status, final ArrayList statusCount) { + if (!statusList.contains(status)) { + statusList.add(status); + statusCount.add(0); + } + } + // } \ No newline at end of file diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java index 7df7e199b4..852a9fae99 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java @@ -21,14 +21,14 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService @Autowired private CounterService counter; - private final List> statusMetric; + private final List> statusMetricsByMinute; private final List statusList; private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public CustomActuatorMetricService() { super(); repo = new InMemoryMetricRepository(); - statusMetric = new ArrayList>(); + statusMetricsByMinute = new ArrayList>(); statusList = new ArrayList(); } @@ -46,7 +46,7 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService public Object[][] getGraphData() { final Date current = new Date(); final int colCount = statusList.size() + 1; - final int rowCount = statusMetric.size() + 1; + final int rowCount = statusMetricsByMinute.size() + 1; final Object[][] result = new Object[rowCount][colCount]; result[0][0] = "Time"; @@ -56,12 +56,15 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService j++; } - List temp; for (int i = 1; i < rowCount; i++) { - temp = statusMetric.get(i - 1); result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); - for (j = 1; j <= temp.size(); j++) { - result[i][j] = temp.get(j - 1); + } + + List minuteOfStatuses; + for (int i = 1; i < rowCount; i++) { + minuteOfStatuses = statusMetricsByMinute.get(i - 1); + for (j = 1; j <= minuteOfStatuses.size(); j++) { + result[i][j] = minuteOfStatuses.get(j - 1); } while (j < colCount) { result[i][j] = 0; @@ -87,6 +90,6 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService } } - statusMetric.add(statusCount); + statusMetricsByMinute.add(statusCount); } } \ No newline at end of file