From 1222077785c6d61226cc9ab54604e2745e77c046 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 19 Aug 2014 01:12:01 +0300 Subject: [PATCH 1/9] cleanup work --- .../persistence/dao/UserRepository.java | 3 +- .../org/baeldung/persistence/model/Role.java | 92 ++++++++++--------- .../org/baeldung/persistence/model/User.java | 20 ++-- .../service/EmailExistsException.java | 4 +- .../service/RepositoryService.java | 22 ++--- .../baeldung/persistence/service/UserDto.java | 17 +++- .../persistence/service/UserService.java | 3 +- .../persistence/service/UserValidator.java | 6 +- .../security/MyUserDetailsService.java | 18 ++-- .../java/org/baeldung/spring/AppConfig.java | 2 +- .../java/org/baeldung/spring/MvcConfig.java | 18 ++-- .../baeldung/spring/PersistenceJPAConfig.java | 4 +- .../baeldung/spring/SecSecurityConfig.java | 2 +- .../controller/RegistrationController.java | 27 +++--- 14 files changed, 121 insertions(+), 117 deletions(-) diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/dao/UserRepository.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/dao/UserRepository.java index d36cd1ba2f..373a28b476 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/dao/UserRepository.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/dao/UserRepository.java @@ -3,7 +3,6 @@ package org.baeldung.persistence.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.baeldung.persistence.model.User; - -public interface UserRepository extends JpaRepository{ +public interface UserRepository extends JpaRepository { public User findByUsername(String username); } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java index f0a08885f5..4be1696e6e 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java @@ -11,52 +11,58 @@ import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; - -@Entity(name="role") +@Entity(name = "role") @Table(name = "role") public class Role { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; - - @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL) - @JoinColumn(name = "user_id") - private User user; - - @Column(name="role") - private Integer role; + @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @JoinColumn(name = "user_id") + private User user; - public Role(){ - super(); - - } - public Role(Integer role){ - super(); - this.role = role; - } - public Role(Integer role, User user){ - super(); - this.role = role; - this.user = user; - } - public Long getId() { - return id; - } - public void setId(Long id) { - this.id = id; - } - public User getUser() { - return user; - } - public void setUser(User user) { - this.user = user; - } - public Integer getRole() { - return role; - } - public void setRole(Integer role) { - this.role = role; - } + @Column(name = "role") + private Integer role; + + public Role() { + super(); + + } + + public Role(Integer role) { + super(); + this.role = role; + } + + public Role(Integer role, User user) { + super(); + this.role = role; + this.user = user; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Integer getRole() { + return role; + } + + public void setRole(Integer role) { + this.role = role; + } } \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/User.java index 4fe8c9986f..33851c9fa5 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/User.java @@ -17,17 +17,16 @@ public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @Column(name="firstName") + @Column(name = "firstName") private String firstName; - @Column(name="lastName") + @Column(name = "lastName") private String lastName; - @Column(name="username") + @Column(name = "username") private String username; - @Column(name="password") + @Column(name = "password") private String password; - - - @OneToOne(mappedBy = "user",fetch = FetchType.EAGER, cascade = CascadeType.ALL) + + @OneToOne(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL) private Role role; public Long getId() { @@ -77,8 +76,7 @@ public class User { public void setRole(Role role) { this.role = role; } - - + @Override public int hashCode() { final int prime = 31; @@ -100,11 +98,11 @@ public class User { return false; return true; } + @Override public String toString() { final StringBuilder builder = new StringBuilder(); - builder.append("User [firstName=").append(firstName).append("]"). - append("[lastName=").append(lastName).append("]").append("[username").append(username).append("]"); + builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(username).append("]"); return builder.toString(); } } \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/EmailExistsException.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/EmailExistsException.java index edb44bd598..ad00388f06 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/EmailExistsException.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/EmailExistsException.java @@ -1,8 +1,8 @@ package org.baeldung.persistence.service; @SuppressWarnings("serial") -public class EmailExistsException extends Throwable{ - +public class EmailExistsException extends Throwable { + public EmailExistsException(String message) { super(message); } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/RepositoryService.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/RepositoryService.java index e5fbc3622d..0cda60da26 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/RepositoryService.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/RepositoryService.java @@ -10,33 +10,33 @@ import org.springframework.stereotype.Service; @Service public class RepositoryService implements UserService { - @Autowired + @Autowired private UserRepository repository; - + @Autowired private Environment env; - + @Autowired - public RepositoryService( UserRepository repository) { + public RepositoryService(UserRepository repository) { this.repository = repository; - } - + } + @Transactional @Override public User registerNewUserAccount(UserDto userAccountData) throws EmailExistsException { if (emailExist(userAccountData.getUsername())) { - + throw new EmailExistsException("There is an account with that email adress: " + userAccountData.getUsername()); - } - User user = new User(); + } + User user = new User(); user.setFirstName(userAccountData.getFirstName()); user.setLastName(userAccountData.getLastName()); user.setPassword(userAccountData.getPassword()); user.setUsername(userAccountData.getUsername()); user.setRole(new Role(userAccountData.getRole(), user)); - return repository.save(user); + return repository.save(user); } - + private boolean emailExist(String email) { User user = repository.findByUsername(email); if (user != null) { diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java index 05056c7eb7..345800201d 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java @@ -5,43 +5,52 @@ public class UserDto { private String lastName; private String password; private String username; - private Integer role; - + private Integer role; + public Integer getRole() { return role; } + public void setRole(Integer role) { this.role = role; } + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + public String getFirstName() { return firstName; } + public void setFirstName(String firstName) { this.firstName = firstName; } + public String getLastName() { return lastName; } + public void setLastName(String lastName) { this.lastName = lastName; } + public String getPassword() { return password; } + public void setPassword(String password) { this.password = password; } + @Override public String toString() { final StringBuilder builder = new StringBuilder(); - builder.append("User [firstName=").append(firstName).append("]"). - append("[lastName=").append(lastName).append("]").append("[username").append(username).append("]").append("[password").append(password).append("]"); + builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(username).append("]").append("[password").append(password).append("]"); return builder.toString(); } } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserService.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserService.java index b51bda5b3b..44187e2c85 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserService.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserService.java @@ -1,8 +1,9 @@ package org.baeldung.persistence.service; + import org.baeldung.persistence.model.User; public interface UserService { - + public User registerNewUserAccount(UserDto userAccountData) throws EmailExistsException; } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserValidator.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserValidator.java index ac6bbb89f3..45d453b934 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserValidator.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserValidator.java @@ -10,18 +10,18 @@ public class UserValidator implements Validator { private Pattern pattern; private Matcher matcher; private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; - + @Override public boolean supports(Class clazz) { return UserDto.class.isAssignableFrom(clazz); } - + @Override public void validate(Object obj, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "message.firstName", "Firstname is required."); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "message.lastName", "LastName is required."); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "message.password", "LastName is required."); - ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "message.username", "UserName is required."); + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "message.username", "UserName is required."); } public boolean validateEmail(String email) { diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-security-login-error-handling/src/main/java/org/baeldung/security/MyUserDetailsService.java index 41c27e2a9c..464a747683 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/security/MyUserDetailsService.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/security/MyUserDetailsService.java @@ -22,33 +22,33 @@ import org.springframework.transaction.annotation.Transactional; public class MyUserDetailsService implements UserDetailsService { private static final Logger LOGGER = LoggerFactory.getLogger(UserDetailsService.class); - + private UserRepository userRepository; @Autowired public MyUserDetailsService(UserRepository repository) { this.userRepository = repository; } - + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { try { LOGGER.debug("Loading user by username: {}", username); User user = userRepository.findByUsername(username); LOGGER.debug("Found user: {}", user); if (user == null) { - //throw new UsernameNotFoundException("No user found with username: " + username); - boolean enabled = false; - return new org.springframework.security.core.userdetails.User(" ", " ", enabled, true, true, true, getAuthorities(new Integer(1))); + // throw new UsernameNotFoundException("No user found with username: " + username); + boolean enabled = false; + return new org.springframework.security.core.userdetails.User(" ", " ", enabled, true, true, true, getAuthorities(new Integer(1))); } boolean enabled = true; boolean accountNonExpired = true; boolean credentialsNonExpired = true; boolean accountNonLocked = true; - return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword().toLowerCase(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(user.getRole().getRole())); + return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword().toLowerCase(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(user.getRole().getRole())); } catch (Exception e) { throw new RuntimeException(e); - } + } } public Collection getAuthorities(Integer role) { @@ -60,7 +60,7 @@ public class MyUserDetailsService implements UserDetailsService { List roles = new ArrayList(); if (role.intValue() == 2) { - // roles.add("ROLE_USER"); + // roles.add("ROLE_USER"); roles.add("ROLE_ADMIN"); } else if (role.intValue() == 1) { @@ -69,7 +69,7 @@ public class MyUserDetailsService implements UserDetailsService { return roles; } - + public static List getGrantedAuthorities(List roles) { List authorities = new ArrayList(); for (String role : roles) { diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/AppConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/AppConfig.java index 6047130a69..73daab8f86 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/AppConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/AppConfig.java @@ -12,7 +12,7 @@ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Import({ MvcConfig.class, PersistenceJPAConfig.class, SecSecurityConfig.class }) @PropertySource("classpath:application.properties") public class AppConfig { - + @Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java index 4747821d1f..59ca9d6765 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java @@ -19,11 +19,8 @@ import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; - @Configuration -@ComponentScan(basePackages = { - "org.baeldung.web.controller", "org.baeldung.persistence.service", "org.baeldung.persistence.dao" -}) +@ComponentScan(basePackages = { "org.baeldung.web.controller", "org.baeldung.persistence.service", "org.baeldung.persistence.dao" }) @EnableWebMvc public class MvcConfig extends WebMvcConfigurerAdapter { @@ -44,9 +41,9 @@ public class MvcConfig extends WebMvcConfigurerAdapter { registry.addViewController("/console.html"); registry.addViewController("/admin.html"); registry.addViewController("/registration.html"); - registry.addViewController("/successRegister.html"); + registry.addViewController("/successRegister.html"); } - + @Bean public ViewResolver viewResolver() { final InternalResourceViewResolver bean = new InternalResourceViewResolver(); @@ -55,11 +52,10 @@ public class MvcConfig extends WebMvcConfigurerAdapter { bean.setSuffix(".jsp"); return bean; } - + @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**") - .addResourceLocations("/","/resources/"); + registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/"); } @Override @@ -85,11 +81,11 @@ public class MvcConfig extends WebMvcConfigurerAdapter { messageSource.setCacheSeconds(0); return messageSource; } - + @Bean public UserValidator userValidator() { UserValidator userValidator = new UserValidator(); return userValidator; } - + } \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java index e4c3d4835f..0baac30ec1 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java @@ -22,7 +22,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @ComponentScan({ "org.baeldung.persistence.model" }) @EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") public class PersistenceJPAConfig { - + @Autowired private Environment env; @@ -69,5 +69,5 @@ public class PersistenceJPAConfig { hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); return hibernateProperties; } - + } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java index 3e793a33f6..4da114c78b 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java @@ -10,5 +10,5 @@ public class SecSecurityConfig { public SecSecurityConfig() { super(); } - + } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java b/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java index a8494efb2d..34637aef49 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java @@ -9,8 +9,6 @@ import org.baeldung.persistence.service.UserValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.MessageSource; -import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; @@ -23,28 +21,25 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.ModelAndView; - @Controller public class RegistrationController { - - private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationController.class); + private final Logger LOGGER = LoggerFactory.getLogger(getClass()); + private UserService service; - @Autowired - private MessageSource messages; - @Autowired - private JavaMailSender mailSender; + @Autowired private UserValidator validator; - + @InitBinder protected void initBinder(WebDataBinder binder) { binder.setValidator(this.validator); } + @Autowired public RegistrationController(UserService service) { this.service = service; } - + @RequestMapping(value = "/user/registration", method = RequestMethod.GET) public String showRegistrationForm(WebRequest request, Model model) { LOGGER.debug("Rendering registration page."); @@ -52,7 +47,7 @@ public class RegistrationController { model.addAttribute("user", userDto); return "registration"; } - + @RequestMapping(value = "/user/registration", method = RequestMethod.POST) public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid UserDto userAccountData, BindingResult result, WebRequest request, Errors errors) { boolean goodEmailCheck = validator.validateEmail(userAccountData.getUsername()); @@ -71,15 +66,15 @@ public class RegistrationController { } } - + private User createUserAccount(UserDto userAccountData, BindingResult result) { User registered = null; try { registered = service.registerNewUserAccount(userAccountData); } catch (EmailExistsException e) { return null; - } + } return registered; - } -} + } +} From 7bee5f637eea95c8b4947af22555a31a17873566 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 19 Aug 2014 17:34:40 +0300 Subject: [PATCH 2/9] rest template work for digest auth and early work on gson --- jackson/pom.xml | 241 +++++++++--------- .../org/baeldung/gson/GenericSourceClass.java | 10 + .../gson/GsonDeserializationUnitTest.java | 32 +++ spring-security-rest-digest-auth/pom.xml | 11 +- ...ntsClientHttpRequestFactoryDigestAuth.java | 5 +- .../baeldung/client/spring/ClientConfig.java | 27 +- .../client/ClientNoSpringLiveTest.java | 64 +++++ ...est.java => ClientWithSpringLiveTest.java} | 13 +- 8 files changed, 264 insertions(+), 139 deletions(-) create mode 100644 jackson/src/test/java/org/baeldung/gson/GenericSourceClass.java create mode 100644 jackson/src/test/java/org/baeldung/gson/GsonDeserializationUnitTest.java create mode 100644 spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientNoSpringLiveTest.java rename spring-security-rest-digest-auth/src/test/java/org/baeldung/client/{ClientLiveTest.java => ClientWithSpringLiveTest.java} (62%) diff --git a/jackson/pom.xml b/jackson/pom.xml index d017c7b65c..e424879a73 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -1,150 +1,157 @@ - - 4.0.0 - org.baeldung - jackson - 0.1-SNAPSHOT + + 4.0.0 + org.baeldung + jackson + 0.1-SNAPSHOT - jackson + jackson - + - + - - com.google.guava - guava - 16.0.1 - - - commons-io - commons-io - 2.4 - + + com.google.guava + guava + 17.0 + + + commons-io + commons-io + 2.4 + - - org.apache.commons - commons-collections4 - 4.0 - + + org.apache.commons + commons-collections4 + 4.0 + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + + com.google.code.gson + gson + 2.3 + - - junit - junit-dep - ${junit.version} - test - + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + junit + junit-dep + ${junit.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - + + org.mockito + mockito-core + ${mockito.version} + test + - - jackson - - - src/main/resources - true - - + - + + jackson + + + src/main/resources + true + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.7 - 1.7 - - + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - + - - - 4.0.6.RELEASE - 3.2.4.RELEASE + - - 4.3.6.Final - 5.1.31 + + + 4.0.6.RELEASE + 3.2.4.RELEASE - - 2.4.1 + + 4.3.6.Final + 5.1.31 - - 1.7.7 - 1.1.2 + + 2.4.1 - - 5.1.2.Final + + 1.7.7 + 1.1.2 - - 17.0 - 3.3.2 + + 5.1.2.Final - - 1.3 - 4.11 - 1.9.5 + + 17.0 + 3.3.2 - 4.3.2 - 4.3.5 + + 1.3 + 4.11 + 1.9.5 - 2.3.2 + 4.3.2 + 4.3.5 - - 3.1 - 2.4 - 2.17 - 2.6 - 1.4.8 + 2.3.2 - + + 3.1 + 2.4 + 2.17 + 2.6 + 1.4.8 + + \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/gson/GenericSourceClass.java b/jackson/src/test/java/org/baeldung/gson/GenericSourceClass.java new file mode 100644 index 0000000000..8e3a8d749b --- /dev/null +++ b/jackson/src/test/java/org/baeldung/gson/GenericSourceClass.java @@ -0,0 +1,10 @@ +package org.baeldung.gson; + +public class GenericSourceClass { + int intField; + + public GenericSourceClass(final int i) { + intField = i; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/gson/GsonDeserializationUnitTest.java b/jackson/src/test/java/org/baeldung/gson/GsonDeserializationUnitTest.java new file mode 100644 index 0000000000..dabc861ce1 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/gson/GsonDeserializationUnitTest.java @@ -0,0 +1,32 @@ +package org.baeldung.gson; + +import org.junit.Before; +import org.junit.Test; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +public class GsonDeserializationUnitTest { + + private Gson gson; + + @Before + public final void before() { + gson = new Gson(); + } + + // tests + + @Test + public void givenUsingGson_whenDeserializingGeneric_thenCorrect() { + final java.lang.reflect.Type genericSourceClassType = new TypeToken() { + }.getType(); + final GenericSourceClass sourceObject = new GenericSourceClass(1); + final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType); + + final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType); + + System.out.println(targetObject); + } + +} diff --git a/spring-security-rest-digest-auth/pom.xml b/spring-security-rest-digest-auth/pom.xml index c9c6617645..9231c745a0 100644 --- a/spring-security-rest-digest-auth/pom.xml +++ b/spring-security-rest-digest-auth/pom.xml @@ -89,7 +89,7 @@ com.fasterxml.jackson.core jackson-databind - 2.2.2 + ${jackson.version} @@ -227,13 +227,13 @@ source - + org.apache.maven.plugins maven-war-plugin ${maven-war-plugin.version} - + org.apache.maven.plugins maven-surefire-plugin @@ -290,6 +290,9 @@ 1.7.7 1.1.2 + + 2.4.2 + 5.1.2.Final @@ -308,7 +311,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java b/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java index d9d89db224..698ab4cb92 100644 --- a/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java +++ b/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java @@ -4,6 +4,7 @@ import java.net.URI; import org.apache.http.HttpHost; import org.apache.http.client.AuthCache; +import org.apache.http.client.HttpClient; import org.apache.http.client.protocol.ClientContext; import org.apache.http.impl.auth.DigestScheme; import org.apache.http.impl.client.BasicAuthCache; @@ -15,8 +16,8 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; public class HttpComponentsClientHttpRequestFactoryDigestAuth extends HttpComponentsClientHttpRequestFactory { HttpHost host; - public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host) { - super(); + public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host, final HttpClient httpClient) { + super(httpClient); this.host = host; } diff --git a/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/spring/ClientConfig.java b/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/spring/ClientConfig.java index 615be8f524..9a3b177500 100644 --- a/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/spring/ClientConfig.java +++ b/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/spring/ClientConfig.java @@ -1,9 +1,13 @@ package org.baeldung.client.spring; import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.baeldung.client.HttpComponentsClientHttpRequestFactoryDigestAuth; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -12,6 +16,8 @@ import org.springframework.web.client.RestTemplate; @Configuration public class ClientConfig { + private static final String DEFAULT_USER = "user1"; + private static final String DEFAULT_PASS = "user1Pass"; public ClientConfig() { super(); @@ -22,7 +28,9 @@ public class ClientConfig { @Bean public RestTemplate restTemplate() { final HttpHost host = new HttpHost("localhost", 8080, "http"); - final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host); + final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).useSystemProperties().build(); + + final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client); final RestTemplate restTemplate = new RestTemplate(requestFactory); final int timeout = 5; @@ -43,9 +51,16 @@ public class ClientConfig { // httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000); // - note: timeout via the API - final HttpParams httpParams = httpClient.getParams(); - HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout - HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout + // final HttpParams httpParams = httpClient.getParams(); + // HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout + // HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout + } + + private final CredentialsProvider provider() { + final CredentialsProvider provider = new BasicCredentialsProvider(); + final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS); + provider.setCredentials(AuthScope.ANY, credentials); + return provider; } } \ No newline at end of file diff --git a/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientNoSpringLiveTest.java b/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientNoSpringLiveTest.java new file mode 100644 index 0000000000..245d5d0a41 --- /dev/null +++ b/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientNoSpringLiveTest.java @@ -0,0 +1,64 @@ +package org.baeldung.client; + +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.baeldung.web.dto.Foo; +import org.junit.Test; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +public class ClientNoSpringLiveTest { + private static final String DEFAULT_USER = "user1"; + private static final String DEFAULT_PASS = "user1Pass"; + + // tests - no Spring + + @Test + public final void givenUsingCustomHttpRequestFactory_whenSecuredRestApiIsConsumed_then200OK() { + final HttpHost host = new HttpHost("localhost", 8080, "http"); + + final CredentialsProvider credentialsProvider = provider(); + final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build(); + final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client); + final RestTemplate restTemplate = new RestTemplate(requestFactory); + + // credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); + + final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; + final ResponseEntity responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); + + System.out.println(responseEntity.getStatusCode()); + } + + @Test + public final void givenUsingStandardRequestFactory_whenSecuredRestApiIsConsumed_then200OK() { + final CredentialsProvider credentialsProvider = provider(); + final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build(); + final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client); + final RestTemplate restTemplate = new RestTemplate(requestFactory); + + // credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); + + final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; + final ResponseEntity responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); + + System.out.println(responseEntity.getStatusCode()); + } + + // UTIL + + private final CredentialsProvider provider() { + final CredentialsProvider provider = new BasicCredentialsProvider(); + final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS); + provider.setCredentials(AuthScope.ANY, credentials); + return provider; + } + +} diff --git a/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientLiveTest.java b/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientWithSpringLiveTest.java similarity index 62% rename from spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientLiveTest.java rename to spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientWithSpringLiveTest.java index 9ab6854f61..b40f9ef472 100644 --- a/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientLiveTest.java +++ b/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientWithSpringLiveTest.java @@ -1,8 +1,5 @@ package org.baeldung.client; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.impl.client.DefaultHttpClient; import org.baeldung.client.spring.ClientConfig; import org.baeldung.web.dto.Foo; import org.junit.Test; @@ -10,7 +7,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; @@ -18,21 +14,18 @@ import org.springframework.web.client.RestTemplate; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class) -public class ClientLiveTest { +public class ClientWithSpringLiveTest { @Autowired private RestTemplate restTemplate; - // tests + // tests - no Spring @Test public final void whenSecuredRestApiIsConsumed_then200OK() { - final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory(); - final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient(); - httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); - final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; final ResponseEntity responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); + System.out.println(responseEntity.getStatusCode()); } From 01625edeae56ad37178e4ed568b77ae55e9333a2 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 23 Aug 2014 10:56:38 +0300 Subject: [PATCH 3/9] initial gson project --- gson/.classpath | 36 +++++ ...e.wst.jsdt.core.javascriptValidator.launch | 7 + gson/.gitignore | 13 ++ gson/.project | 36 +++++ gson/.springBeans | 14 ++ gson/README.md | 12 ++ gson/pom.xml | 151 ++++++++++++++++++ gson/src/main/resources/example1.json | 12 ++ gson/src/main/resources/example2.json | 10 ++ gson/src/main/resources/logback.xml | 20 +++ gson/src/main/webapp/WEB-INF/api-servlet.xml | 6 + gson/src/main/webapp/WEB-INF/web.xml | 42 +++++ .../deserialization/GenericSourceClass.java | 10 ++ .../test/GsonDeserializationUnitTest.java | 33 ++++ gson/src/test/resources/.gitignore | 13 ++ 15 files changed, 415 insertions(+) create mode 100644 gson/.classpath create mode 100644 gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch create mode 100644 gson/.gitignore create mode 100644 gson/.project create mode 100644 gson/.springBeans create mode 100644 gson/README.md create mode 100644 gson/pom.xml create mode 100644 gson/src/main/resources/example1.json create mode 100644 gson/src/main/resources/example2.json create mode 100644 gson/src/main/resources/logback.xml create mode 100644 gson/src/main/webapp/WEB-INF/api-servlet.xml create mode 100644 gson/src/main/webapp/WEB-INF/web.xml create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java create mode 100644 gson/src/test/resources/.gitignore diff --git a/gson/.classpath b/gson/.classpath new file mode 100644 index 0000000000..0720e4851b --- /dev/null +++ b/gson/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch new file mode 100644 index 0000000000..627021fb96 --- /dev/null +++ b/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch @@ -0,0 +1,7 @@ + + + + + + + diff --git a/gson/.gitignore b/gson/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/gson/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/gson/.project b/gson/.project new file mode 100644 index 0000000000..ea8ae1a67f --- /dev/null +++ b/gson/.project @@ -0,0 +1,36 @@ + + + jackson + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/gson/.springBeans b/gson/.springBeans new file mode 100644 index 0000000000..a79097f40d --- /dev/null +++ b/gson/.springBeans @@ -0,0 +1,14 @@ + + + 1 + + + + + + + src/main/webapp/WEB-INF/api-servlet.xml + + + + diff --git a/gson/README.md b/gson/README.md new file mode 100644 index 0000000000..539cb34761 --- /dev/null +++ b/gson/README.md @@ -0,0 +1,12 @@ +========= + +## Jackson Cookbooks and Examples + +### Relevant Articles: +- [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization) +- [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array) +- [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties) +- [Jackson – Custom Serializer](http://www.baeldung.com/jackson-custom-serialization) +- [Jackson – Custom Deserializer](http://www.baeldung.com/jackson-deserialization) + + diff --git a/gson/pom.xml b/gson/pom.xml new file mode 100644 index 0000000000..2a28f4fb36 --- /dev/null +++ b/gson/pom.xml @@ -0,0 +1,151 @@ + + 4.0.0 + org.baeldung + gson + 0.1-SNAPSHOT + + gson + + + + + + + com.google.guava + guava + 17.0 + + + commons-io + commons-io + 2.4 + + + + org.apache.commons + commons-collections4 + 4.0 + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + + + + com.google.code.gson + gson + 2.3 + + + + + + junit + junit-dep + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + jackson + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + 4.0.6.RELEASE + 3.2.4.RELEASE + + + 4.3.6.Final + 5.1.31 + + + 2.4.1 + + + 1.7.7 + 1.1.2 + + + 5.1.2.Final + + + 17.0 + 3.3.2 + + + 1.3 + 4.11 + 1.9.5 + + 4.3.2 + 4.3.5 + + 2.3.2 + + + 3.1 + 2.4 + 2.17 + 2.6 + 1.4.8 + + + + \ No newline at end of file diff --git a/gson/src/main/resources/example1.json b/gson/src/main/resources/example1.json new file mode 100644 index 0000000000..46d2982cec --- /dev/null +++ b/gson/src/main/resources/example1.json @@ -0,0 +1,12 @@ +{ + "collection": [ + { + "name": "Test order1", + "detail": "ahk ks" + }, + { + "name": "Test order2", + "detail": "Fisteku" + } + ] +} \ No newline at end of file diff --git a/gson/src/main/resources/example2.json b/gson/src/main/resources/example2.json new file mode 100644 index 0000000000..f4433731e6 --- /dev/null +++ b/gson/src/main/resources/example2.json @@ -0,0 +1,10 @@ +[ + { + "name": "Test order1", + "detail": "ahk ks" + }, + { + "name": "Test order2", + "detail": "Fisteku" + } +] \ No newline at end of file diff --git a/gson/src/main/resources/logback.xml b/gson/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/gson/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/gson/src/main/webapp/WEB-INF/api-servlet.xml b/gson/src/main/webapp/WEB-INF/api-servlet.xml new file mode 100644 index 0000000000..a675fc6d95 --- /dev/null +++ b/gson/src/main/webapp/WEB-INF/api-servlet.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/gson/src/main/webapp/WEB-INF/web.xml b/gson/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..48d4b8fe61 --- /dev/null +++ b/gson/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,42 @@ + + + + Spring MVC Application + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.config + + + + org.springframework.web.context.ContextLoaderListener + + + + + api + org.springframework.web.servlet.DispatcherServlet + 1 + + + api + / + + + + + + + \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java new file mode 100644 index 0000000000..590203e622 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java @@ -0,0 +1,10 @@ +package org.baeldung.gson.deserialization; + +public class GenericSourceClass { + int intField; + + public GenericSourceClass(final int i) { + intField = i; + } + +} \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java new file mode 100644 index 0000000000..583aa16ff7 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java @@ -0,0 +1,33 @@ +package org.baeldung.gson.deserialization.test; + +import org.baeldung.gson.deserialization.GenericSourceClass; +import org.junit.Before; +import org.junit.Test; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +public class GsonDeserializationUnitTest { + + private Gson gson; + + @Before + public final void before() { + gson = new Gson(); + } + + // tests + + @Test + public void givenUsingGson_whenDeserializingGeneric_thenCorrect() { + final java.lang.reflect.Type genericSourceClassType = new TypeToken() { + }.getType(); + final GenericSourceClass sourceObject = new GenericSourceClass(1); + final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType); + + final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType); + + System.out.println(targetObject); + } + +} diff --git a/gson/src/test/resources/.gitignore b/gson/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/gson/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file From ade4f1470280f656b36dcbf0f7f1376ae0283697 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 23 Aug 2014 11:00:20 +0300 Subject: [PATCH 4/9] further gson testing work --- gson/README.md | 8 +- gson/pom.xml | 6 +- .../deserialization/GenericSourceClass.java | 10 -- .../deserialization/GenericTargetClass.java | 18 ++++ .../gson/deserialization/SourceClass.java | 32 ++++++ .../SourceClassDeserializer.java | 25 +++++ .../gson/deserialization/TargetClass.java | 19 ++++ .../TargetClassDeserializer.java | 21 ++++ .../test/GsonDeserializationTest.java | 98 +++++++++++++++++++ .../test/GsonDeserializationUnitTest.java | 33 ------- 10 files changed, 217 insertions(+), 53 deletions(-) delete mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/GenericTargetClass.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java delete mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java diff --git a/gson/README.md b/gson/README.md index 539cb34761..559e536d81 100644 --- a/gson/README.md +++ b/gson/README.md @@ -1,12 +1,6 @@ ========= -## Jackson Cookbooks and Examples +## GSON Cookbooks and Examples ### Relevant Articles: -- [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization) -- [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array) -- [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties) -- [Jackson – Custom Serializer](http://www.baeldung.com/jackson-custom-serialization) -- [Jackson – Custom Deserializer](http://www.baeldung.com/jackson-deserialization) - diff --git a/gson/pom.xml b/gson/pom.xml index 2a28f4fb36..6b93c0c1f5 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -41,7 +41,7 @@ com.google.code.gson gson - 2.3 + ${gson.version} @@ -76,7 +76,7 @@ - jackson + gson src/main/resources @@ -116,7 +116,7 @@ 5.1.31 - 2.4.1 + 2.3 1.7.7 diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java deleted file mode 100644 index 590203e622..0000000000 --- a/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.baeldung.gson.deserialization; - -public class GenericSourceClass { - int intField; - - public GenericSourceClass(final int i) { - intField = i; - } - -} \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/GenericTargetClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/GenericTargetClass.java new file mode 100644 index 0000000000..9ff569f9e3 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/GenericTargetClass.java @@ -0,0 +1,18 @@ +package org.baeldung.gson.deserialization; + +public class GenericTargetClass { + + public INTEGER intField; + + GenericTargetClass(final INTEGER value) { + intField = value; + } + + // + + @Override + public String toString() { + return "GenericTargetClass{" + "intField=" + intField + '}'; + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java new file mode 100644 index 0000000000..5561a15500 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java @@ -0,0 +1,32 @@ +package org.baeldung.gson.deserialization; + +public class SourceClass { + int intValue; + String stringValue; + + public SourceClass(int intValue, String stringValue) { + this.intValue = intValue; + this.stringValue = stringValue; + } + + @Override + public String toString() { + return "SourceClass{" + + "intValue=" + intValue + + ", stringValue='" + stringValue + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SourceClass)) return false; + + SourceClass that = (SourceClass) o; + + if (intValue != that.intValue) return false; + if (!stringValue.equals(that.stringValue)) return false; + + return true; + } +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java b/gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java new file mode 100644 index 0000000000..1d82acabf8 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java @@ -0,0 +1,25 @@ +package org.baeldung.gson.deserialization; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; + +public class SourceClassDeserializer implements JsonDeserializer { + + @Override + public SourceClass[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonArray jArray = json.getAsJsonArray(); + SourceClass[] scArray = new SourceClass[jArray.size()]; + int index = 0; + for (JsonElement jElement : jArray) { + int i = jElement.getAsJsonObject().get("intValue").getAsInt(); + String s = jElement.getAsJsonObject().get("stringValue").getAsString(); + scArray[index++] = new SourceClass(i, s); + } + return scArray; + } +} \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java new file mode 100644 index 0000000000..8181f2d9c2 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java @@ -0,0 +1,19 @@ +package org.baeldung.gson.deserialization; + +public class TargetClass { + public int intValue; + public String stringValue; + + public TargetClass(final int intValue, final String stringValue) { + this.intValue = intValue; + this.stringValue = stringValue; + } + + // API + + @Override + public String toString() { + return "TargetClass{" + "intValue= " + intValue + ", stringValue= " + stringValue + '}'; + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java b/gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java new file mode 100644 index 0000000000..e19f7d3b82 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java @@ -0,0 +1,21 @@ +package org.baeldung.gson.deserialization; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; + + +public class TargetClassDeserializer implements JsonDeserializer { + + @Override + public TargetClass deserialize(JsonElement jElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject jObject = jElement.getAsJsonObject(); + int intValue = jObject.get("valueInt").getAsInt(); + String stringValue = jObject.get("valueString").getAsString(); + return new TargetClass(intValue, stringValue); + } +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java new file mode 100644 index 0000000000..3cb9639520 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java @@ -0,0 +1,98 @@ +package org.baeldung.gson.deserialization.test; + +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.baeldung.gson.deserialization.GenericTargetClass; +import org.baeldung.gson.deserialization.SourceClass; +import org.baeldung.gson.deserialization.SourceClassDeserializer; +import org.baeldung.gson.deserialization.TargetClass; +import org.baeldung.gson.deserialization.TargetClassDeserializer; +import org.junit.Test; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; + +public class GsonDeserializationTest { + + @Test + public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenUsingCustomDeserializer_thenCorrect() { + final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}"; + final GsonBuilder gsonBldr = new GsonBuilder(); + gsonBldr.registerTypeAdapter(TargetClass.class, new TargetClassDeserializer()); + final Gson gson = gsonBldr.create(); + final TargetClass targetObject = gson.fromJson(jsonSourceObject, TargetClass.class); + + assertEquals(targetObject.intValue, 7); + assertEquals(targetObject.stringValue, "seven"); + } + + @Test + public void givenJsonWithArray_whenUsingGsonCustomDeserializer_thenMapsToArrayList() { + // It is necessary to override the equals() method in SourceClass + final String jsonSourceObject = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; + final GsonBuilder gsonBldr = new GsonBuilder(); + gsonBldr.registerTypeHierarchyAdapter(SourceClass[].class, new SourceClassDeserializer()); + final Gson gson = gsonBldr.create(); + + final List targetList = Arrays.asList(gson.fromJson(jsonSourceObject, SourceClass[].class)); + + assertEquals(new SourceClass(1, "one"), targetList.get(0)); + } + + @Test + public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenDeserializingManualy_thenCorrect() { + final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}"; + final JsonParser jParser = new JsonParser(); + final JsonElement jElement = jParser.parse(jsonSourceObject); + final JsonObject jObject = jElement.getAsJsonObject(); + final int intValue = jObject.get("valueInt").getAsInt(); + final String stringValue = jObject.get("valueString").getAsString(); + + final TargetClass targetObject = new TargetClass(intValue, stringValue); + + assertEquals(targetObject.intValue, 7); + assertEquals(targetObject.stringValue, "seven"); + } + + @Test + public void givenJsonHasExtraValuesButGsonIsIgnoringExtras_whenDeserializing_thenCorrect() { + final String serializedSourceObject = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}"; + final TargetClass targetObject = new Gson().fromJson(serializedSourceObject, TargetClass.class); + + assertEquals(targetObject.intValue, 1); + assertEquals(targetObject.stringValue, "one"); + } + + @Test + public void givenUsingGson_whenDeserializingGeneric_thenCorrect() { + final Type genericTargetClassType = new TypeToken>() { + }.getType(); + final String serializedSourceObject = "{\"intField\":1}"; + + final GenericTargetClass targetObject = new Gson().fromJson(serializedSourceObject, genericTargetClassType); + + assertEquals(targetObject.intField, new Integer(1)); + } + + @Test + public void givenUsingGson_whenDeserializingCollection_thenCorrect() { + final String serializedSourceCollection = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; + final Type targetClassType = new TypeToken>() { + }.getType(); + + final Collection targetCollection = new Gson().fromJson(serializedSourceCollection, targetClassType); + assertThat(targetCollection, instanceOf(ArrayList.class)); + } +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java deleted file mode 100644 index 583aa16ff7..0000000000 --- a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.baeldung.gson.deserialization.test; - -import org.baeldung.gson.deserialization.GenericSourceClass; -import org.junit.Before; -import org.junit.Test; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - -public class GsonDeserializationUnitTest { - - private Gson gson; - - @Before - public final void before() { - gson = new Gson(); - } - - // tests - - @Test - public void givenUsingGson_whenDeserializingGeneric_thenCorrect() { - final java.lang.reflect.Type genericSourceClassType = new TypeToken() { - }.getType(); - final GenericSourceClass sourceObject = new GenericSourceClass(1); - final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType); - - final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType); - - System.out.println(targetObject); - } - -} From 2e48d26ff571d2a01d9d0f79dbd14806cb97d309 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 23 Aug 2014 12:50:27 +0300 Subject: [PATCH 5/9] deserialization work --- gson/src/main/resources/example1.json | 12 --- gson/src/main/resources/example2.json | 10 -- .../baeldung/gson/deserialization/Foo.java | 47 ++++++++ .../gson/deserialization/FooDeserializer.java | 26 +++++ ...serializerFromJsonWithDifferentFields.java | 21 ++++ ...enericTargetClass.java => GenericFoo.java} | 4 +- .../gson/deserialization/SourceClass.java | 32 ------ .../SourceClassDeserializer.java | 25 ----- .../gson/deserialization/TargetClass.java | 19 ---- .../TargetClassDeserializer.java | 21 ---- .../test/GsonDeserializationTest.java | 101 +++++++++--------- 11 files changed, 149 insertions(+), 169 deletions(-) delete mode 100644 gson/src/main/resources/example1.json delete mode 100644 gson/src/main/resources/example2.json create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/Foo.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializer.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializerFromJsonWithDifferentFields.java rename gson/src/test/java/org/baeldung/gson/deserialization/{GenericTargetClass.java => GenericFoo.java} (72%) delete mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java delete mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java delete mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java delete mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java diff --git a/gson/src/main/resources/example1.json b/gson/src/main/resources/example1.json deleted file mode 100644 index 46d2982cec..0000000000 --- a/gson/src/main/resources/example1.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "collection": [ - { - "name": "Test order1", - "detail": "ahk ks" - }, - { - "name": "Test order2", - "detail": "Fisteku" - } - ] -} \ No newline at end of file diff --git a/gson/src/main/resources/example2.json b/gson/src/main/resources/example2.json deleted file mode 100644 index f4433731e6..0000000000 --- a/gson/src/main/resources/example2.json +++ /dev/null @@ -1,10 +0,0 @@ -[ - { - "name": "Test order1", - "detail": "ahk ks" - }, - { - "name": "Test order2", - "detail": "Fisteku" - } -] \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java b/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java new file mode 100644 index 0000000000..1663fd6fa6 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java @@ -0,0 +1,47 @@ +package org.baeldung.gson.deserialization; + +public class Foo { + public int intValue; + public String stringValue; + + public Foo(final int intValue, final String stringValue) { + this.intValue = intValue; + this.stringValue = stringValue; + } + + // API + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + intValue; + result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final Foo other = (Foo) obj; + if (intValue != other.intValue) + return false; + if (stringValue == null) { + if (other.stringValue != null) + return false; + } else if (!stringValue.equals(other.stringValue)) + return false; + return true; + } + + @Override + public String toString() { + return "TargetClass{" + "intValue= " + intValue + ", stringValue= " + stringValue + '}'; + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializer.java b/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializer.java new file mode 100644 index 0000000000..17c1dfae19 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializer.java @@ -0,0 +1,26 @@ +package org.baeldung.gson.deserialization; + +import java.lang.reflect.Type; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +public class FooDeserializer implements JsonDeserializer { + + @Override + public Foo[] deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { + final JsonArray jArray = json.getAsJsonArray(); + final Foo[] scArray = new Foo[jArray.size()]; + int index = 0; + for (final JsonElement jElement : jArray) { + final int i = jElement.getAsJsonObject().get("intValue").getAsInt(); + final String s = jElement.getAsJsonObject().get("stringValue").getAsString(); + scArray[index++] = new Foo(i, s); + } + return scArray; + } + +} \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializerFromJsonWithDifferentFields.java b/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializerFromJsonWithDifferentFields.java new file mode 100644 index 0000000000..d4eaa39ac5 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializerFromJsonWithDifferentFields.java @@ -0,0 +1,21 @@ +package org.baeldung.gson.deserialization; + +import java.lang.reflect.Type; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +public class FooDeserializerFromJsonWithDifferentFields implements JsonDeserializer { + + @Override + public Foo deserialize(final JsonElement jElement, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { + final JsonObject jObject = jElement.getAsJsonObject(); + final int intValue = jObject.get("valueInt").getAsInt(); + final String stringValue = jObject.get("valueString").getAsString(); + return new Foo(intValue, stringValue); + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/GenericTargetClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java similarity index 72% rename from gson/src/test/java/org/baeldung/gson/deserialization/GenericTargetClass.java rename to gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java index 9ff569f9e3..0ab4d18da2 100644 --- a/gson/src/test/java/org/baeldung/gson/deserialization/GenericTargetClass.java +++ b/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java @@ -1,10 +1,10 @@ package org.baeldung.gson.deserialization; -public class GenericTargetClass { +public class GenericFoo { public INTEGER intField; - GenericTargetClass(final INTEGER value) { + GenericFoo(final INTEGER value) { intField = value; } diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java deleted file mode 100644 index 5561a15500..0000000000 --- a/gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.baeldung.gson.deserialization; - -public class SourceClass { - int intValue; - String stringValue; - - public SourceClass(int intValue, String stringValue) { - this.intValue = intValue; - this.stringValue = stringValue; - } - - @Override - public String toString() { - return "SourceClass{" + - "intValue=" + intValue + - ", stringValue='" + stringValue + '\'' + - '}'; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof SourceClass)) return false; - - SourceClass that = (SourceClass) o; - - if (intValue != that.intValue) return false; - if (!stringValue.equals(that.stringValue)) return false; - - return true; - } -} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java b/gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java deleted file mode 100644 index 1d82acabf8..0000000000 --- a/gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.baeldung.gson.deserialization; - -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; - -import java.lang.reflect.Type; - -public class SourceClassDeserializer implements JsonDeserializer { - - @Override - public SourceClass[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - JsonArray jArray = json.getAsJsonArray(); - SourceClass[] scArray = new SourceClass[jArray.size()]; - int index = 0; - for (JsonElement jElement : jArray) { - int i = jElement.getAsJsonObject().get("intValue").getAsInt(); - String s = jElement.getAsJsonObject().get("stringValue").getAsString(); - scArray[index++] = new SourceClass(i, s); - } - return scArray; - } -} \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java deleted file mode 100644 index 8181f2d9c2..0000000000 --- a/gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.baeldung.gson.deserialization; - -public class TargetClass { - public int intValue; - public String stringValue; - - public TargetClass(final int intValue, final String stringValue) { - this.intValue = intValue; - this.stringValue = stringValue; - } - - // API - - @Override - public String toString() { - return "TargetClass{" + "intValue= " + intValue + ", stringValue= " + stringValue + '}'; - } - -} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java b/gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java deleted file mode 100644 index e19f7d3b82..0000000000 --- a/gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.baeldung.gson.deserialization; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import java.lang.reflect.Type; - - -public class TargetClassDeserializer implements JsonDeserializer { - - @Override - public TargetClass deserialize(JsonElement jElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - JsonObject jObject = jElement.getAsJsonObject(); - int intValue = jObject.get("valueInt").getAsInt(); - String stringValue = jObject.get("valueString").getAsString(); - return new TargetClass(intValue, stringValue); - } -} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java index 3cb9639520..3868790f96 100644 --- a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java +++ b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java @@ -1,6 +1,8 @@ package org.baeldung.gson.deserialization.test; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -10,11 +12,9 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; -import org.baeldung.gson.deserialization.GenericTargetClass; -import org.baeldung.gson.deserialization.SourceClass; -import org.baeldung.gson.deserialization.SourceClassDeserializer; -import org.baeldung.gson.deserialization.TargetClass; -import org.baeldung.gson.deserialization.TargetClassDeserializer; +import org.baeldung.gson.deserialization.Foo; +import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields; +import org.baeldung.gson.deserialization.GenericFoo; import org.junit.Test; import com.google.gson.Gson; @@ -26,33 +26,67 @@ import com.google.gson.reflect.TypeToken; public class GsonDeserializationTest { + // tests - single element + @Test - public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenUsingCustomDeserializer_thenCorrect() { + public final void givenJsonHasExtraValuesButGsonIsIgnoringExtras_whenDeserializing_thenCorrect() { + final String serializedSourceObject = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}"; + final Foo targetObject = new Gson().fromJson(serializedSourceObject, Foo.class); + + assertEquals(targetObject.intValue, 1); + assertEquals(targetObject.stringValue, "one"); + } + + @Test + public final void givenJsonHasNonMatchingFieldNames_whenDeserializingWithCustomDeserializer_thenCorrect() { final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}"; + final GsonBuilder gsonBldr = new GsonBuilder(); - gsonBldr.registerTypeAdapter(TargetClass.class, new TargetClassDeserializer()); - final Gson gson = gsonBldr.create(); - final TargetClass targetObject = gson.fromJson(jsonSourceObject, TargetClass.class); + gsonBldr.registerTypeAdapter(Foo.class, new FooDeserializerFromJsonWithDifferentFields()); + final Foo targetObject = gsonBldr.create().fromJson(jsonSourceObject, Foo.class); assertEquals(targetObject.intValue, 7); assertEquals(targetObject.stringValue, "seven"); } @Test - public void givenJsonWithArray_whenUsingGsonCustomDeserializer_thenMapsToArrayList() { - // It is necessary to override the equals() method in SourceClass + public final void givenUsingGson_whenDeserializingGeneric_thenCorrect() { + final Type genericTargetClassType = new TypeToken>() { + }.getType(); + final String serializedSourceObject = "{\"intField\":1}"; + + final GenericFoo targetObject = new Gson().fromJson(serializedSourceObject, genericTargetClassType); + + assertEquals(targetObject.intField, new Integer(1)); + } + + // tests - multiple elements + + @Test + public final void givenJsonArrayOfFoos_whenDeserializingToList_thenCorrect() { final String jsonSourceObject = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; - final GsonBuilder gsonBldr = new GsonBuilder(); - gsonBldr.registerTypeHierarchyAdapter(SourceClass[].class, new SourceClassDeserializer()); - final Gson gson = gsonBldr.create(); + final Foo[] objectsAsArray = new GsonBuilder().create().fromJson(jsonSourceObject, Foo[].class); + final List targetList = Arrays.asList(objectsAsArray); - final List targetList = Arrays.asList(gson.fromJson(jsonSourceObject, SourceClass[].class)); - - assertEquals(new SourceClass(1, "one"), targetList.get(0)); + assertThat(targetList, hasItem(new Foo(1, "one"))); + assertThat(targetList, hasItem(new Foo(2, "two"))); + assertThat(targetList, not(hasItem(new Foo(1, "two")))); } @Test - public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenDeserializingManualy_thenCorrect() { + public final void givenUsingGson_whenDeserializingCollection_thenCorrect() { + final String serializedSourceCollection = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; + final Type targetClassType = new TypeToken>() { + }.getType(); + + final Collection targetCollection = new Gson().fromJson(serializedSourceCollection, targetClassType); + assertThat(targetCollection, instanceOf(ArrayList.class)); + } + + // + + @Test + public void whenDeserializingJsonIntoElements_thenCorrect() { final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}"; final JsonParser jParser = new JsonParser(); final JsonElement jElement = jParser.parse(jsonSourceObject); @@ -60,39 +94,10 @@ public class GsonDeserializationTest { final int intValue = jObject.get("valueInt").getAsInt(); final String stringValue = jObject.get("valueString").getAsString(); - final TargetClass targetObject = new TargetClass(intValue, stringValue); + final Foo targetObject = new Foo(intValue, stringValue); assertEquals(targetObject.intValue, 7); assertEquals(targetObject.stringValue, "seven"); } - @Test - public void givenJsonHasExtraValuesButGsonIsIgnoringExtras_whenDeserializing_thenCorrect() { - final String serializedSourceObject = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}"; - final TargetClass targetObject = new Gson().fromJson(serializedSourceObject, TargetClass.class); - - assertEquals(targetObject.intValue, 1); - assertEquals(targetObject.stringValue, "one"); - } - - @Test - public void givenUsingGson_whenDeserializingGeneric_thenCorrect() { - final Type genericTargetClassType = new TypeToken>() { - }.getType(); - final String serializedSourceObject = "{\"intField\":1}"; - - final GenericTargetClass targetObject = new Gson().fromJson(serializedSourceObject, genericTargetClassType); - - assertEquals(targetObject.intField, new Integer(1)); - } - - @Test - public void givenUsingGson_whenDeserializingCollection_thenCorrect() { - final String serializedSourceCollection = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; - final Type targetClassType = new TypeToken>() { - }.getType(); - - final Collection targetCollection = new Gson().fromJson(serializedSourceCollection, targetClassType); - assertThat(targetCollection, instanceOf(ArrayList.class)); - } } From c00ef532c96255a472e919141ba1dd802799dbab Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 23 Aug 2014 13:26:09 +0300 Subject: [PATCH 6/9] gson work --- .../baeldung/gson/deserialization/Foo.java | 4 ++ .../gson/deserialization/GenericFoo.java | 12 ++--- .../test/GsonDeserializationTest.java | 54 +++++++++++-------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java b/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java index 1663fd6fa6..64720f63f9 100644 --- a/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java +++ b/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java @@ -9,6 +9,10 @@ public class Foo { this.stringValue = stringValue; } + public Foo() { + super(); + } + // API @Override diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java b/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java index 0ab4d18da2..954cf007cf 100644 --- a/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java +++ b/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java @@ -1,18 +1,18 @@ package org.baeldung.gson.deserialization; -public class GenericFoo { +public class GenericFoo { - public INTEGER intField; + public T theValue; - GenericFoo(final INTEGER value) { - intField = value; + public GenericFoo(final T value) { + theValue = value; } // @Override - public String toString() { - return "GenericTargetClass{" + "intField=" + intField + '}'; + public final String toString() { + return "GenericTargetClass{" + "intField=" + theValue + '}'; } } diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java index 3868790f96..342b3096d7 100644 --- a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java +++ b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java @@ -8,15 +8,14 @@ import static org.junit.Assert.assertThat; import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.List; import org.baeldung.gson.deserialization.Foo; import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields; import org.baeldung.gson.deserialization.GenericFoo; import org.junit.Test; +import com.google.common.collect.Lists; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; @@ -29,57 +28,66 @@ public class GsonDeserializationTest { // tests - single element @Test - public final void givenJsonHasExtraValuesButGsonIsIgnoringExtras_whenDeserializing_thenCorrect() { - final String serializedSourceObject = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}"; - final Foo targetObject = new Gson().fromJson(serializedSourceObject, Foo.class); + public final void whenDeserializingToSimpleObject_thenCorrect() { + final String json = "{\"intValue\":1,\"stringValue\":\"one\"}"; + + final Foo targetObject = new Gson().fromJson(json, Foo.class); assertEquals(targetObject.intValue, 1); assertEquals(targetObject.stringValue, "one"); } @Test - public final void givenJsonHasNonMatchingFieldNames_whenDeserializingWithCustomDeserializer_thenCorrect() { - final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}"; + public final void givenJsonHasExtraValues_whenDeserializing_thenCorrect() { + final String json = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}"; + final Foo targetObject = new Gson().fromJson(json, Foo.class); + + assertEquals(targetObject.intValue, 1); + assertEquals(targetObject.stringValue, "one"); + } + + @Test + public final void givenJsonHasNonMatchingFields_whenDeserializingWithCustomDeserializer_thenCorrect() { + final String json = "{\"valueInt\":7,\"valueString\":\"seven\"}"; final GsonBuilder gsonBldr = new GsonBuilder(); gsonBldr.registerTypeAdapter(Foo.class, new FooDeserializerFromJsonWithDifferentFields()); - final Foo targetObject = gsonBldr.create().fromJson(jsonSourceObject, Foo.class); + final Foo targetObject = gsonBldr.create().fromJson(json, Foo.class); assertEquals(targetObject.intValue, 7); assertEquals(targetObject.stringValue, "seven"); } @Test - public final void givenUsingGson_whenDeserializingGeneric_thenCorrect() { - final Type genericTargetClassType = new TypeToken>() { + public final void whenDeserializingToGenericObject_thenCorrect() { + final Type typeToken = new TypeToken>() { }.getType(); - final String serializedSourceObject = "{\"intField\":1}"; + final String json = "{\"theValue\":1}"; - final GenericFoo targetObject = new Gson().fromJson(serializedSourceObject, genericTargetClassType); + final GenericFoo targetObject = new Gson().fromJson(json, typeToken); - assertEquals(targetObject.intField, new Integer(1)); + assertEquals(targetObject.theValue, new Integer(1)); } // tests - multiple elements @Test - public final void givenJsonArrayOfFoos_whenDeserializingToList_thenCorrect() { - final String jsonSourceObject = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; - final Foo[] objectsAsArray = new GsonBuilder().create().fromJson(jsonSourceObject, Foo[].class); - final List targetList = Arrays.asList(objectsAsArray); + public final void givenJsonArrayOfFoos_whenDeserializingToArray_thenCorrect() { + final String json = "[{\"intValue\":1,\"stringValue\":\"one\"}," + "{\"intValue\":2,\"stringValue\":\"two\"}]"; + final Foo[] targetArray = new GsonBuilder().create().fromJson(json, Foo[].class); - assertThat(targetList, hasItem(new Foo(1, "one"))); - assertThat(targetList, hasItem(new Foo(2, "two"))); - assertThat(targetList, not(hasItem(new Foo(1, "two")))); + assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(1, "one"))); + assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(2, "two"))); + assertThat(Lists.newArrayList(targetArray), not(hasItem(new Foo(1, "two")))); } @Test - public final void givenUsingGson_whenDeserializingCollection_thenCorrect() { - final String serializedSourceCollection = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; + public final void givenJsonArrayOfFoos_whenDeserializingCollection_thenCorrect() { + final String json = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; final Type targetClassType = new TypeToken>() { }.getType(); - final Collection targetCollection = new Gson().fromJson(serializedSourceCollection, targetClassType); + final Collection targetCollection = new Gson().fromJson(json, targetClassType); assertThat(targetCollection, instanceOf(ArrayList.class)); } From b30114fc26a6b5e04ed69a577331ecadc772a800 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 23 Aug 2014 14:09:45 +0300 Subject: [PATCH 7/9] minor maven upgrades --- core-java/pom.xml | 6 +++--- experiments/pom.xml | 6 +++--- gson/pom.xml | 6 +++--- guava/pom.xml | 6 +++--- httpclient/pom.xml | 6 +++--- jackson/pom.xml | 6 +++--- mockito/pom.xml | 6 +++--- sandbox/pom.xml | 2 +- spring-all/pom.xml | 6 +++--- spring-exceptions/pom.xml | 6 +++--- spring-hibernate3/pom.xml | 6 +++--- spring-hibernate4/pom.xml | 6 +++--- spring-jpa/pom.xml | 6 +++--- spring-mvc-java/pom.xml | 6 +++--- spring-mvc-no-xml/pom.xml | 2 +- spring-mvc-xml/pom.xml | 2 +- spring-rest/pom.xml | 6 +++--- spring-security-basic-auth/pom.xml | 6 +++--- spring-security-login-error-handling/pom.xml | 6 +++--- spring-security-mvc-custom/pom.xml | 6 +++--- spring-security-mvc-digest-auth/pom.xml | 6 +++--- spring-security-mvc-login/pom.xml | 6 +++--- spring-security-mvc-persisted-remember-me/pom.xml | 6 +++--- spring-security-mvc-session/pom.xml | 6 +++--- spring-security-rest-basic-auth/pom.xml | 6 +++--- spring-security-rest-custom/pom.xml | 6 +++--- spring-security-rest-digest-auth/pom.xml | 4 ++-- spring-security-rest-full/pom.xml | 6 +++--- spring-security-rest/pom.xml | 6 +++--- 29 files changed, 80 insertions(+), 80 deletions(-) diff --git a/core-java/pom.xml b/core-java/pom.xml index 27daf5f0f8..131a328834 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -146,11 +146,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 2.4.1 @@ -181,7 +181,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/experiments/pom.xml b/experiments/pom.xml index f814928f4a..b7794ef2f4 100644 --- a/experiments/pom.xml +++ b/experiments/pom.xml @@ -256,11 +256,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -288,7 +288,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/gson/pom.xml b/gson/pom.xml index 6b93c0c1f5..9c041f0cc1 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -109,11 +109,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 2.3 @@ -144,7 +144,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/guava/pom.xml b/guava/pom.xml index 18142c12f2..0297b706b6 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -95,11 +95,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -127,7 +127,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/httpclient/pom.xml b/httpclient/pom.xml index 6aa5f96344..529fdb2c38 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -150,11 +150,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -182,7 +182,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/jackson/pom.xml b/jackson/pom.xml index e424879a73..7d7b6421a4 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -115,11 +115,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 2.4.1 @@ -150,7 +150,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/mockito/pom.xml b/mockito/pom.xml index 8829e2e2d1..3ad055690a 100644 --- a/mockito/pom.xml +++ b/mockito/pom.xml @@ -90,11 +90,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -122,7 +122,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/sandbox/pom.xml b/sandbox/pom.xml index 4f7fb39e2f..57734d7393 100644 --- a/sandbox/pom.xml +++ b/sandbox/pom.xml @@ -134,7 +134,7 @@ 4.0.3.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final diff --git a/spring-all/pom.xml b/spring-all/pom.xml index 9932b68278..07c842aaad 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -193,13 +193,13 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 3.18.1-GA 1.2 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -227,7 +227,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-exceptions/pom.xml b/spring-exceptions/pom.xml index 0025469340..1c0c06062f 100644 --- a/spring-exceptions/pom.xml +++ b/spring-exceptions/pom.xml @@ -205,13 +205,13 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 3.18.1-GA 1.2 4.3.6.Final - 5.1.31 + 5.1.32 7.0.42 @@ -241,7 +241,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-hibernate3/pom.xml b/spring-hibernate3/pom.xml index cdcc2277f9..23ab319e24 100644 --- a/spring-hibernate3/pom.xml +++ b/spring-hibernate3/pom.xml @@ -164,12 +164,12 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 3.18.1-GA 3.6.10.Final - 5.1.31 + 5.1.32 7.0.47 @@ -197,7 +197,7 @@ 3.1 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-hibernate4/pom.xml b/spring-hibernate4/pom.xml index c16fff764a..47c5c13d7e 100644 --- a/spring-hibernate4/pom.xml +++ b/spring-hibernate4/pom.xml @@ -171,12 +171,12 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 3.18.1-GA 4.3.6.Final - 5.1.31 + 5.1.32 7.0.42 @@ -204,7 +204,7 @@ 3.1 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-jpa/pom.xml b/spring-jpa/pom.xml index ddffa5101e..b2d3525dcc 100644 --- a/spring-jpa/pom.xml +++ b/spring-jpa/pom.xml @@ -166,11 +166,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -197,7 +197,7 @@ 3.1 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 7e966a699e..d9c27481d4 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -142,11 +142,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -174,7 +174,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-mvc-no-xml/pom.xml b/spring-mvc-no-xml/pom.xml index f2ab27e251..264879437c 100644 --- a/spring-mvc-no-xml/pom.xml +++ b/spring-mvc-no-xml/pom.xml @@ -166,7 +166,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index c957869c12..df23cf654c 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -168,7 +168,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index a5044a675b..f59a8128da 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -203,11 +203,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 @@ -238,7 +238,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-basic-auth/pom.xml b/spring-security-basic-auth/pom.xml index fea2ae9ce5..6ce841c954 100644 --- a/spring-security-basic-auth/pom.xml +++ b/spring-security-basic-auth/pom.xml @@ -227,11 +227,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -259,7 +259,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-login-error-handling/pom.xml b/spring-security-login-error-handling/pom.xml index 53d446cc99..4224ab29b3 100644 --- a/spring-security-login-error-handling/pom.xml +++ b/spring-security-login-error-handling/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.1.4.RELEASE + 1.1.5.RELEASE @@ -142,7 +142,7 @@ 1.7 3.1.1.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 1.6.10 @@ -156,7 +156,7 @@ 1 - 1.4.1.RELEASE + 1.4.5.RELEASE 17.0 diff --git a/spring-security-mvc-custom/pom.xml b/spring-security-mvc-custom/pom.xml index 0dd81b1edc..b8e7f4b5a8 100644 --- a/spring-security-mvc-custom/pom.xml +++ b/spring-security-mvc-custom/pom.xml @@ -232,11 +232,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -264,7 +264,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-mvc-digest-auth/pom.xml b/spring-security-mvc-digest-auth/pom.xml index 8dc1748fb4..cf6a001b12 100644 --- a/spring-security-mvc-digest-auth/pom.xml +++ b/spring-security-mvc-digest-auth/pom.xml @@ -227,11 +227,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -259,7 +259,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-mvc-login/pom.xml b/spring-security-mvc-login/pom.xml index 65f14c0432..fa16d2c393 100644 --- a/spring-security-mvc-login/pom.xml +++ b/spring-security-mvc-login/pom.xml @@ -224,11 +224,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -256,7 +256,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-mvc-persisted-remember-me/pom.xml b/spring-security-mvc-persisted-remember-me/pom.xml index 8e0491d78b..caf00b6d98 100644 --- a/spring-security-mvc-persisted-remember-me/pom.xml +++ b/spring-security-mvc-persisted-remember-me/pom.xml @@ -261,11 +261,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -293,7 +293,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-mvc-session/pom.xml b/spring-security-mvc-session/pom.xml index 9285a243c4..95dfcf9fdf 100644 --- a/spring-security-mvc-session/pom.xml +++ b/spring-security-mvc-session/pom.xml @@ -232,11 +232,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -264,7 +264,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest-basic-auth/pom.xml b/spring-security-rest-basic-auth/pom.xml index b44169f368..fc273049ba 100644 --- a/spring-security-rest-basic-auth/pom.xml +++ b/spring-security-rest-basic-auth/pom.xml @@ -288,11 +288,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 4.3.2 @@ -320,7 +320,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest-custom/pom.xml b/spring-security-rest-custom/pom.xml index 2189396535..80a37c9080 100644 --- a/spring-security-rest-custom/pom.xml +++ b/spring-security-rest-custom/pom.xml @@ -252,11 +252,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -283,7 +283,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest-digest-auth/pom.xml b/spring-security-rest-digest-auth/pom.xml index 9231c745a0..6a8124879c 100644 --- a/spring-security-rest-digest-auth/pom.xml +++ b/spring-security-rest-digest-auth/pom.xml @@ -276,11 +276,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 4.3.2 diff --git a/spring-security-rest-full/pom.xml b/spring-security-rest-full/pom.xml index 1ffe7244a6..d76b90b4f2 100644 --- a/spring-security-rest-full/pom.xml +++ b/spring-security-rest-full/pom.xml @@ -372,11 +372,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.6.2.RELEASE @@ -408,7 +408,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest/pom.xml b/spring-security-rest/pom.xml index c23901bda6..b85fd3565a 100644 --- a/spring-security-rest/pom.xml +++ b/spring-security-rest/pom.xml @@ -239,11 +239,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -270,7 +270,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 From 039d3c0af39e357ad3e3f60b65f0e2ceffd3ee55 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 23 Aug 2014 15:10:43 +0300 Subject: [PATCH 8/9] cleanup work for registrationg --- .../src/main/webapp/WEB-INF/view/admin.jsp | 15 +++----- .../src/main/webapp/WEB-INF/view/console.jsp | 9 +++-- .../src/main/webapp/WEB-INF/view/home.jsp | 14 +++---- .../src/main/webapp/WEB-INF/view/homepage.jsp | 7 +--- .../webapp/WEB-INF/view/invalidSession.jsp | 11 +++--- .../src/main/webapp/WEB-INF/view/login.jsp | 38 +++++++++---------- .../src/main/webapp/WEB-INF/view/logout.jsp | 8 ++-- .../main/webapp/WEB-INF/view/registration.jsp | 12 +++--- .../webapp/WEB-INF/view/successRegister.jsp | 13 ++++--- 9 files changed, 64 insertions(+), 63 deletions(-) diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp index 027b387b96..c8300c8476 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp @@ -1,22 +1,19 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> - - + + " rel="stylesheet"> - - - -

Hello Admin

-
- +

Hello Admin

+
+ ">Logout ">Home diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp index b82842169f..b6ddea4d52 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp @@ -1,6 +1,7 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%> + " rel="stylesheet"> @@ -10,16 +11,16 @@ This text is only visible to a user -
+
This text is only visible to an admin -
+
">Logout ">Administrator Page - + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp index 4d54257bff..fbde8700a6 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp @@ -1,14 +1,14 @@ -<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> -<%@ page session="true" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +<%@ page session="true"%> + " rel="stylesheet"> - Home +Home - -

- Welcome back home! -

+ +

Welcome back home!

+ diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp index c13acb7756..7b8d96b255 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp @@ -1,13 +1,12 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> -<%@ page session="true" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> +<%@ page session="true"%> " rel="stylesheet"> -

This is the homepage for the user

@@ -23,8 +22,6 @@ ">Logout ">Home ">Administrator Page - - \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp index 8aa9d7b49f..6c46cd3936 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp @@ -3,12 +3,13 @@ " rel="stylesheet"> - Home +Home - -

- -

+ +

+ +

+ diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp index 97c8b3317d..c8b60e9a3e 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp @@ -1,6 +1,5 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" - uri="http://www.springframework.org/security/tags"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> @@ -9,6 +8,7 @@ + " rel="stylesheet"> @@ -29,7 +29,6 @@ document.f.j_username.focus(); return false; } - if (document.f.j_password.value == "") { alert("${noPass}"); document.f.j_password.focus(); @@ -43,27 +42,26 @@

Login

English | Spanish -
+ - - - - - - - - - - - - -
User:
Password:
+ + + + + + + + + + + + +
User:
Password:
-
+
Current Locale : ${pageContext.response.locale}
">Sign Up - + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp index a234f11d09..b3a154b36c 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp @@ -1,9 +1,9 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" - uri="http://www.springframework.org/security/tags"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> + " rel="stylesheet"> @@ -19,8 +19,10 @@ Logged Out + - + Login + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp index 979da11427..d0d72e0ded 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp @@ -11,28 +11,29 @@ Registration +

This is the registration page

- +
- + - + - + - + @@ -41,4 +42,5 @@
">Back to Login + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp index ca676a4999..8932b6dae1 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp @@ -1,20 +1,23 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" - uri="http://www.springframework.org/security/tags"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ page session="true"%> + " rel="stylesheet"> Registration Success - -

- "> + +

+ +

+ "> + \ No newline at end of file From 6106a9f6ddc0790b5512658e4a26a669e5eab429 Mon Sep 17 00:00:00 2001 From: eugenp Date: Mon, 25 Aug 2014 01:26:25 +0300 Subject: [PATCH 9/9] minor form change --- spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp index e0ae61dd7b..588678cdcf 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp @@ -8,7 +8,7 @@

Welcome, Enter The Employee Details

- +
Name