Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
b292a48657
|
@ -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.
|
||||
Standalone java programs to measure the performance of both JSON APIs based on file size and object graph complexity.
|
||||
|
|
|
@ -14,13 +14,6 @@
|
|||
<version>${json-path.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- utilities -->
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@ -44,13 +37,24 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- json-path -->
|
||||
<json-path.version>2.1.0</json-path.version>
|
||||
|
||||
<!-- utilities -->
|
||||
<joda-time.version>2.9.2</joda-time.version>
|
||||
|
||||
<!-- Testing -->
|
||||
<junit.version>4.12</junit.version>
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
]
|
|
@ -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<String> dataSourceUsername = jsonContext.read("$[?(@.username == '" + extractedUsername + "')]");
|
||||
if (dataSourceUsername.size() == 0)
|
||||
return Result.FAILURE;
|
||||
|
||||
Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build();
|
||||
List<String> pathToCurrentUser = JsonPath.using(pathConfiguration).parse(jsonDataSourceString).read("$[?(@.username == '" + extractedUsername + "')]");
|
||||
List<Long> 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;
|
||||
}
|
||||
}
|
|
@ -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<String> list = JsonPath.parse(jsonDataSourceString).read("$[?(@.username == '" + extractedUsername + "' && @.password.current.value == '" + extractedPassword + "')]");
|
||||
|
||||
if (list.size() == 0)
|
||||
return Result.FAILURE;
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String> userDataSource = JsonPath.parse(jsonDataSourceString).read("$[*]['username']");
|
||||
String extractedUsername = JsonPath.parse(requestBody).read("$['username']");
|
||||
|
||||
if (userDataSource.toString().contains(extractedUsername))
|
||||
return Result.FAILURE;
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
}
|
|
@ -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<Map<String, Object>> 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<Object> 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<String> pathList = JsonPath.using(pathConfiguration).parse(jsonString).read("$[?(@['box office'] == " + highestRevenue + ")]");
|
||||
|
||||
Map<String, String> 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<Map<String, Object>> dataList = context.read("$[?(@.director == 'Sam Mendes')]");
|
||||
|
||||
List<Object> dateList = new ArrayList<>();
|
||||
for (Map<String, Object> 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<Map<String, Object>> finalDataList = context.read("$[?(@['director'] == 'Sam Mendes' && @['release date'] == " + latestTime + ")]");
|
||||
String title = (String) finalDataList.get(0).get("title");
|
||||
|
||||
assertEquals("Spectre", title);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<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>org.baeldung</groupId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<artifactId>spring-mvc-xml</artifactId>
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.spring;
|
||||
package com.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.spring;
|
||||
package com.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
|
@ -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;
|
|
@ -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<String> favouriteLanguageItem = new ArrayList<String>();
|
||||
favouriteLanguageItem.add("Java");
|
||||
favouriteLanguageItem.add("C++");
|
||||
favouriteLanguageItem.add("Perl");
|
||||
model.addAttribute("favouriteLanguageItem", favouriteLanguageItem);
|
||||
|
||||
final List<String> jobItem = new ArrayList<String>();
|
||||
jobItem.add("Full time");
|
||||
jobItem.add("Part time");
|
||||
model.addAttribute("jobItem", jobItem);
|
||||
|
||||
final Map<String, String> countryItems = new LinkedHashMap<String, String>();
|
||||
countryItems.put("US", "United Stated");
|
||||
countryItems.put("IT", "Italy");
|
||||
countryItems.put("UK", "United Kingdom");
|
||||
countryItems.put("FR", "Grance");
|
||||
model.addAttribute("countryItems", countryItems);
|
||||
|
||||
final List<String> fruit = new ArrayList<String>();
|
||||
fruit.add("Banana");
|
||||
fruit.add("Mango");
|
||||
fruit.add("Apple");
|
||||
model.addAttribute("fruit", fruit);
|
||||
|
||||
final List<String> books = new ArrayList<String>();
|
||||
books.add("The Great Gatsby");
|
||||
books.add("Nineteen Eighty-Four");
|
||||
books.add("The Lord of the Rings");
|
||||
model.addAttribute("books", books);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.spring.form;
|
||||
package com.baeldung.spring.form;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
|
@ -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<String> favouriteLanguage;
|
||||
private List<String> 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<String> getFavouriteLanguage() {
|
||||
return favouriteLanguage;
|
||||
}
|
||||
|
||||
public void setFavouriteLanguage(final List<String> favouriteLanguage) {
|
||||
this.favouriteLanguage = favouriteLanguage;
|
||||
}
|
||||
|
||||
public String getNotes() {
|
||||
return notes;
|
||||
}
|
||||
|
||||
public void setNotes(final String notes) {
|
||||
this.notes = notes;
|
||||
}
|
||||
|
||||
public List<String> getFruit() {
|
||||
return fruit;
|
||||
}
|
||||
|
||||
public void setFruit(final List<String> 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;
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
required.name = Name is required!
|
||||
NotEmpty.person.password = Password is required!
|
|
@ -11,7 +11,9 @@
|
|||
>
|
||||
|
||||
<mvc:annotation-driven/>
|
||||
<context:component-scan base-package="org.baeldung.spring.controller"/>
|
||||
|
||||
<context:component-scan base-package="com.baeldung.spring.controller"/>
|
||||
|
||||
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/WEB-INF/view/"/>
|
||||
<property name="suffix" value=".jsp"/>
|
||||
|
@ -19,4 +21,15 @@
|
|||
|
||||
<mvc:view-controller path="/sample.html" view-name="sample"/>
|
||||
|
||||
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
|
||||
<property name="basename" value="messages" />
|
||||
</bean>
|
||||
|
||||
<!-- <bean id="multipartResolver"
|
||||
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> -->
|
||||
|
||||
<bean id="multipartResolver"
|
||||
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -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"%>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Form Example - Register a Person</title>
|
||||
</head>
|
||||
|
||||
<style>
|
||||
|
||||
.error {
|
||||
color: #ff0000;
|
||||
}
|
||||
|
||||
.errorbox {
|
||||
|
||||
background-color: #ffEEEE;
|
||||
border: 2px solid #ff0000;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
<body>
|
||||
|
||||
<h3>Welcome, Enter The Person Details</h3>
|
||||
|
||||
<form:form method="POST" action="addPerson" modelAttribute="person" enctype="multipart/form-data">
|
||||
|
||||
<form:errors path="*" cssClass="errorbox" element="div" />
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td><form:label path="name">Name</form:label></td>
|
||||
<td><form:input path="name" /></td>
|
||||
<td><form:errors path="name" cssClass="error" element="div"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="email">E-mail</form:label></td>
|
||||
<td><form:input type="email" path="email" /></td>
|
||||
<td><form:errors path="email" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="dateOfBirth">Date of birth</form:label></td>
|
||||
<td><form:input type="date" path="dateOfBirth" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="password">Password</form:label></td>
|
||||
<td><form:password path="password" /></td>
|
||||
<td><form:errors path="password" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="sex">Sex</form:label></td>
|
||||
<td>
|
||||
Male: <form:radiobutton path="sex" value="M"/> <br/>
|
||||
Female: <form:radiobutton path="sex" value="F"/>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="job">Job</form:label></td>
|
||||
<td>
|
||||
<form:radiobuttons items="${jobItem}" path="job" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="country">Country</form:label></td>
|
||||
<td>
|
||||
<form:select path="country" items="${countryItems}" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="book">Book</form:label></td>
|
||||
<td>
|
||||
<form:select path="book">
|
||||
<form:option value="-" label="--Please Select"/>
|
||||
<form:options items="${books}" />
|
||||
</form:select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="fruit">Fruit</form:label></td>
|
||||
<td>
|
||||
<form:select path="fruit" items="${fruit}" multiple="true"/>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="receiveNewsletter">Receive newsletter</form:label></td>
|
||||
<td><form:checkbox path="receiveNewsletter" rows="3" cols="20"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="hobbies">Hobbies</form:label></td>
|
||||
<td>
|
||||
Bird watching: <form:checkbox path="hobbies" value="Bird watching"/>
|
||||
Astronomy: <form:checkbox path="hobbies" value="Astronomy"/>
|
||||
Snowboarding: <form:checkbox path="hobbies" value="Snowboarding"/>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="favouriteLanguage">Favourite languages</form:label></td>
|
||||
<td>
|
||||
<form:checkboxes items="${favouriteLanguageItem}" path="favouriteLanguage" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="notes">Notes</form:label></td>
|
||||
<td><form:textarea path="notes" rows="3" cols="20"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="file">Select a file to upload</form:label></td>
|
||||
<td><input type="file" name="file" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:hidden path="id" value="12345"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
</form:form>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -0,0 +1,80 @@
|
|||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring MVC Form Handling</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Submitted Person Information</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td>Id :</td>
|
||||
<td>${person.id}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Name :</td>
|
||||
<td>${person.name}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Date of birth :</td>
|
||||
<td>${person.dateOfBirth}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Password :</td>
|
||||
<td>${person.password}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Sex :</td>
|
||||
<td>${person.sex}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Job :</td>
|
||||
<td>${person.job}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Country :</td>
|
||||
<td>${person.country}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Fruit :</td>
|
||||
<td><c:forEach items="${person.fruit}" var="current">[<c:out value="${current}" />]</c:forEach></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Book :</td>
|
||||
<td>${person.book}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Receive Newsletter :</td>
|
||||
<td>${person.receiveNewsletter}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Hobbies :</td>
|
||||
<td><c:forEach items="${person.hobbies}" var="current">[<c:out value="${current}" />]</c:forEach></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Favourite Languages :</td>
|
||||
<td><c:forEach items="${person.favouriteLanguage}" var="current">[<c:out value="${current}" />]</c:forEach></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Notes :</td>
|
||||
<td>${person.notes}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2>Submitted File</h2>
|
||||
<table>
|
||||
|
||||
<tr>
|
||||
<td>OriginalFileName :</td>
|
||||
<td>${person.file.originalFilename}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Type :</td>
|
||||
<td>${person.file.contentType}</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
|
@ -16,7 +16,7 @@
|
|||
</context-param>
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>org.baeldung.spring</param-value>
|
||||
<param-value>com.baeldung.spring</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
|
@ -28,6 +28,9 @@
|
|||
<servlet-name>mvc</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
<multipart-config>
|
||||
<location>/tmp</location>
|
||||
</multipart-config>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>mvc</servlet-name>
|
||||
|
|
|
@ -17,13 +17,13 @@ public class ActuatorMetricService implements IActuatorMetricService {
|
|||
@Autowired
|
||||
private MetricReaderPublicMetrics publicMetrics;
|
||||
|
||||
private final List<ArrayList<Integer>> statusMetric;
|
||||
private final List<ArrayList<Integer>> statusMetricsByMinute;
|
||||
private final List<String> statusList;
|
||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
public ActuatorMetricService() {
|
||||
super();
|
||||
statusMetric = new ArrayList<ArrayList<Integer>>();
|
||||
statusMetricsByMinute = new ArrayList<ArrayList<Integer>>();
|
||||
statusList = new ArrayList<String>();
|
||||
}
|
||||
|
||||
|
@ -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<Integer> temp;
|
||||
List<Integer> last = new ArrayList<Integer>();
|
||||
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<Integer> minuteOfStatuses;
|
||||
List<Integer> last = new ArrayList<Integer>();
|
||||
|
||||
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<Integer> statusCount = initializeCounterList(statusList.size());
|
||||
final ArrayList<Integer> lastMinuteStatuses = initializeStatuses(statusList.size());
|
||||
|
||||
for (final Metric<?> counterMetric : publicMetrics.metrics()) {
|
||||
updateStatusCount(counterMetric, statusCount);
|
||||
updateMetrics(counterMetric, lastMinuteStatuses);
|
||||
}
|
||||
|
||||
statusMetric.add(statusCount);
|
||||
statusMetricsByMinute.add(lastMinuteStatuses);
|
||||
}
|
||||
|
||||
private ArrayList<Integer> initializeCounterList(final int size) {
|
||||
private ArrayList<Integer> initializeStatuses(final int size) {
|
||||
final ArrayList<Integer> counterList = new ArrayList<Integer>();
|
||||
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<Integer> statusCount) {
|
||||
private void updateMetrics(final Metric<?> counterMetric, final ArrayList<Integer> 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<Integer> statusCount) {
|
||||
if (!statusList.contains(status)) {
|
||||
statusList.add(status);
|
||||
statusCount.add(0);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
}
|
|
@ -21,14 +21,14 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService
|
|||
@Autowired
|
||||
private CounterService counter;
|
||||
|
||||
private final List<ArrayList<Integer>> statusMetric;
|
||||
private final List<ArrayList<Integer>> statusMetricsByMinute;
|
||||
private final List<String> statusList;
|
||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
public CustomActuatorMetricService() {
|
||||
super();
|
||||
repo = new InMemoryMetricRepository();
|
||||
statusMetric = new ArrayList<ArrayList<Integer>>();
|
||||
statusMetricsByMinute = new ArrayList<ArrayList<Integer>>();
|
||||
statusList = new ArrayList<String>();
|
||||
}
|
||||
|
||||
|
@ -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<Integer> 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<Integer> 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue