Merge remote-tracking branch 'refs/remotes/eugenp/master' into XStream-ObjectToJson
This commit is contained in:
commit
63f1bd7665
@ -2,22 +2,21 @@ package com.baeldung.unzip;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
public class UnzipFile {
|
public class UnzipFile {
|
||||||
public static void main(String[] args) throws FileNotFoundException, IOException {
|
public static void main(final String[] args) throws IOException {
|
||||||
String fileZip = "/opt/zipped/cities.zip";
|
final String fileZip = "src/main/resources/compressed.zip";
|
||||||
byte[] buffer = new byte[1024];
|
final byte[] buffer = new byte[1024];
|
||||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
|
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
|
||||||
ZipEntry zipEntry = zis.getNextEntry();
|
ZipEntry zipEntry = zis.getNextEntry();
|
||||||
while(zipEntry != null){
|
while(zipEntry != null){
|
||||||
String fileName = zipEntry.getName();
|
final String fileName = zipEntry.getName();
|
||||||
File newFile = new File("/opt/unzipped/" + fileName);
|
final File newFile = new File("src/main/resources/unzipTest/" + fileName);
|
||||||
FileOutputStream fos = new FileOutputStream(newFile);
|
final FileOutputStream fos = new FileOutputStream(newFile);
|
||||||
int len;
|
int len;
|
||||||
while ((len = zis.read(buffer)) > 0) {
|
while ((len = zis.read(buffer)) > 0) {
|
||||||
fos.write(buffer, 0, len);
|
fos.write(buffer, 0, len);
|
||||||
|
43
core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java
Normal file
43
core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.zip;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
public class ZipDirectory {
|
||||||
|
public static void main(final String[] args) throws IOException {
|
||||||
|
final String sourceFile = "src/main/resources/zipTest";
|
||||||
|
final FileOutputStream fos = new FileOutputStream("src/main/resources/dirCompressed.zip");
|
||||||
|
final ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||||
|
final File fileToZip = new File(sourceFile);
|
||||||
|
|
||||||
|
zipFile(fileToZip, fileToZip.getName(), zipOut);
|
||||||
|
zipOut.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void zipFile(final File fileToZip, final String fileName, final ZipOutputStream zipOut) throws IOException {
|
||||||
|
if (fileToZip.isHidden()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (fileToZip.isDirectory()) {
|
||||||
|
final File[] children = fileToZip.listFiles();
|
||||||
|
for (final File childFile : children) {
|
||||||
|
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final FileInputStream fis = new FileInputStream(fileToZip);
|
||||||
|
final ZipEntry zipEntry = new ZipEntry(fileName);
|
||||||
|
zipOut.putNextEntry(zipEntry);
|
||||||
|
final byte[] bytes = new byte[1024];
|
||||||
|
int length;
|
||||||
|
while ((length = fis.read(bytes)) >= 0) {
|
||||||
|
zipOut.write(bytes, 0, length);
|
||||||
|
}
|
||||||
|
fis.close();
|
||||||
|
}
|
||||||
|
}
|
@ -2,22 +2,21 @@ package com.baeldung.zip;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
public class ZipFile {
|
public class ZipFile {
|
||||||
public static void main(String[] args) throws FileNotFoundException, IOException {
|
public static void main(final String[] args) throws IOException {
|
||||||
String sourceFile = "/opt/photos/photo.png";
|
final String sourceFile = "src/main/resources/zipTest/test1.txt";
|
||||||
FileOutputStream fos = new FileOutputStream("/opt/zipped/cities.zip");
|
final FileOutputStream fos = new FileOutputStream("src/main/resources/compressed.zip");
|
||||||
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
final ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||||
File fileToZip = new File(sourceFile);
|
final File fileToZip = new File(sourceFile);
|
||||||
FileInputStream fis = new FileInputStream(fileToZip);
|
final FileInputStream fis = new FileInputStream(fileToZip);
|
||||||
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
final ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
||||||
zipOut.putNextEntry(zipEntry);
|
zipOut.putNextEntry(zipEntry);
|
||||||
byte[] bytes = new byte[1024];
|
final byte[] bytes = new byte[1024];
|
||||||
int length;
|
int length;
|
||||||
while((length = fis.read(bytes)) >= 0) {
|
while((length = fis.read(bytes)) >= 0) {
|
||||||
zipOut.write(bytes, 0, length);
|
zipOut.write(bytes, 0, length);
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.zip;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
public class ZipMultipleFiles {
|
||||||
|
public static void main(final String[] args) throws IOException {
|
||||||
|
final List<String> srcFiles = Arrays.asList("src/main/resources/zipTest/test1.txt", "src/main/resources/zipTest/test2.txt");
|
||||||
|
final FileOutputStream fos = new FileOutputStream("src/main/resources/multiCompressed.zip");
|
||||||
|
final ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||||
|
for (final String srcFile : srcFiles) {
|
||||||
|
final File fileToZip = new File(srcFile);
|
||||||
|
final FileInputStream fis = new FileInputStream(fileToZip);
|
||||||
|
final ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
||||||
|
zipOut.putNextEntry(zipEntry);
|
||||||
|
|
||||||
|
final byte[] bytes = new byte[1024];
|
||||||
|
int length;
|
||||||
|
while((length = fis.read(bytes)) >= 0) {
|
||||||
|
zipOut.write(bytes, 0, length);
|
||||||
|
}
|
||||||
|
fis.close();
|
||||||
|
}
|
||||||
|
zipOut.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
BIN
core-java-8/src/main/resources/compressed.zip
Normal file
BIN
core-java-8/src/main/resources/compressed.zip
Normal file
Binary file not shown.
BIN
core-java-8/src/main/resources/dirCompressed.zip
Normal file
BIN
core-java-8/src/main/resources/dirCompressed.zip
Normal file
Binary file not shown.
BIN
core-java-8/src/main/resources/multiCompressed.zip
Normal file
BIN
core-java-8/src/main/resources/multiCompressed.zip
Normal file
Binary file not shown.
1
core-java-8/src/main/resources/unzipTest/test1.txt
Normal file
1
core-java-8/src/main/resources/unzipTest/test1.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello World!
|
1
core-java-8/src/main/resources/zipTest/test1.txt
Normal file
1
core-java-8/src/main/resources/zipTest/test1.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello World!
|
1
core-java-8/src/main/resources/zipTest/test2.txt
Normal file
1
core-java-8/src/main/resources/zipTest/test2.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
My Name is John
|
@ -0,0 +1 @@
|
|||||||
|
My Name is Tom
|
@ -0,0 +1 @@
|
|||||||
|
My Name is Jane
|
@ -1,5 +1,7 @@
|
|||||||
package org.baeldung.java.io;
|
package org.baeldung.java.io;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
@ -23,6 +25,8 @@ public class JavaXToWriterUnitTest {
|
|||||||
final Writer targetWriter = new StringWriter().append(new String(initialArray));
|
final Writer targetWriter = new StringWriter().append(new String(initialArray));
|
||||||
|
|
||||||
targetWriter.close();
|
targetWriter.close();
|
||||||
|
|
||||||
|
assertEquals("With Java", targetWriter.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -40,6 +44,8 @@ public class JavaXToWriterUnitTest {
|
|||||||
charSink.write(buffer);
|
charSink.write(buffer);
|
||||||
|
|
||||||
stringWriter.close();
|
stringWriter.close();
|
||||||
|
|
||||||
|
assertEquals("With Guava", stringWriter.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -48,6 +54,8 @@ public class JavaXToWriterUnitTest {
|
|||||||
final Writer targetWriter = new StringBuilderWriter(new StringBuilder(new String(initialArray)));
|
final Writer targetWriter = new StringBuilderWriter(new StringBuilder(new String(initialArray)));
|
||||||
|
|
||||||
targetWriter.close();
|
targetWriter.close();
|
||||||
|
|
||||||
|
assertEquals("With Commons IO", targetWriter.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,13 @@
|
|||||||
<artifactId>slf4j-log4j12</artifactId>
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
<version>${org.slf4j.version}</version>
|
<version>${org.slf4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- common -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-fileupload</groupId>
|
||||||
|
<artifactId>commons-fileupload</artifactId>
|
||||||
|
<version>1.3.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.aop;
|
package com.baeldung.aop;
|
||||||
|
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
@ -27,11 +27,11 @@ public class LoggingAspect {
|
|||||||
public void repositoryMethods() {
|
public void repositoryMethods() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Pointcut("@annotation(org.baeldung.aop.annotations.Loggable)")
|
@Pointcut("@annotation(com.baeldung.aop.annotations.Loggable)")
|
||||||
public void loggableMethods() {
|
public void loggableMethods() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Pointcut("@args(org.baeldung.aop.annotations.Entity)")
|
@Pointcut("@args(com.baeldung.aop.annotations.Entity)")
|
||||||
public void methodsAcceptingEntities() {
|
public void methodsAcceptingEntities() {
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.aop;
|
package com.baeldung.aop;
|
||||||
|
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Around;
|
@ -1,10 +1,10 @@
|
|||||||
package org.baeldung.aop;
|
package com.baeldung.aop;
|
||||||
|
|
||||||
|
import com.baeldung.events.FooCreationEvent;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterReturning;
|
import org.aspectj.lang.annotation.AfterReturning;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
import org.baeldung.events.FooCreationEvent;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.aop.annotations;
|
package com.baeldung.aop.annotations;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.aop.annotations;
|
package com.baeldung.aop.annotations;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
@ -1,7 +1,7 @@
|
|||||||
package org.baeldung.dao;
|
package com.baeldung.dao;
|
||||||
|
|
||||||
import org.baeldung.aop.annotations.Loggable;
|
import com.baeldung.aop.annotations.Loggable;
|
||||||
import org.baeldung.model.Foo;
|
import com.baeldung.model.Foo;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
@ -1,9 +1,9 @@
|
|||||||
package org.baeldung.dialect;
|
package com.baeldung.dialect;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.baeldung.processor.NameProcessor;
|
import com.baeldung.processor.NameProcessor;
|
||||||
import org.thymeleaf.dialect.AbstractDialect;
|
import org.thymeleaf.dialect.AbstractDialect;
|
||||||
import org.thymeleaf.processor.IProcessor;
|
import org.thymeleaf.processor.IProcessor;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.events;
|
package com.baeldung.events;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.events;
|
package com.baeldung.events;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.model;
|
package com.baeldung.model;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
package org.baeldung.model;
|
package com.baeldung.model;
|
||||||
|
|
||||||
import org.baeldung.aop.annotations.Entity;
|
import com.baeldung.aop.annotations.Entity;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Foo {
|
public class Foo {
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.model;
|
package com.baeldung.model;
|
||||||
|
|
||||||
public class User {
|
public class User {
|
||||||
private String firstname;
|
private String firstname;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.processor;
|
package com.baeldung.processor;
|
||||||
|
|
||||||
import org.thymeleaf.Arguments;
|
import org.thymeleaf.Arguments;
|
||||||
import org.thymeleaf.dom.Element;
|
import org.thymeleaf.dom.Element;
|
@ -0,0 +1,93 @@
|
|||||||
|
package com.baeldung.spring.web.config;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.baeldung.dialect.CustomDialect;
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Description;
|
||||||
|
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||||
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||||
|
import org.springframework.web.servlet.view.JstlView;
|
||||||
|
import org.thymeleaf.dialect.IDialect;
|
||||||
|
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||||
|
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||||
|
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
||||||
|
|
||||||
|
@EnableWebMvc
|
||||||
|
@Configuration
|
||||||
|
public class ClientWebConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
|
public ClientWebConfig() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||||
|
super.addViewControllers(registry);
|
||||||
|
|
||||||
|
registry.addViewController("/sample.html");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ViewResolver thymeleafViewResolver() {
|
||||||
|
final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||||
|
viewResolver.setTemplateEngine(templateEngine());
|
||||||
|
viewResolver.setOrder(1);
|
||||||
|
return viewResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ViewResolver viewResolver() {
|
||||||
|
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||||
|
bean.setViewClass(JstlView.class);
|
||||||
|
bean.setPrefix("/WEB-INF/view/");
|
||||||
|
bean.setSuffix(".jsp");
|
||||||
|
bean.setOrder(0);
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Description("Thymeleaf template resolver serving HTML 5")
|
||||||
|
public ServletContextTemplateResolver templateResolver() {
|
||||||
|
final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
||||||
|
templateResolver.setPrefix("/WEB-INF/templates/");
|
||||||
|
templateResolver.setSuffix(".html");
|
||||||
|
templateResolver.setTemplateMode("HTML5");
|
||||||
|
return templateResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Description("Thymeleaf template engine with Spring integration")
|
||||||
|
public SpringTemplateEngine templateEngine() {
|
||||||
|
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||||
|
templateEngine.setTemplateResolver(templateResolver());
|
||||||
|
final Set<IDialect> dialects = new HashSet<>();
|
||||||
|
dialects.add(new CustomDialect());
|
||||||
|
templateEngine.setAdditionalDialects(dialects);
|
||||||
|
return templateEngine;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Description("Spring message resolver")
|
||||||
|
public MessageSource messageSource() {
|
||||||
|
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||||
|
messageSource.setBasename("messages");
|
||||||
|
return messageSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
|
||||||
|
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.spring.web.config;
|
package com.baeldung.spring.web.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.spring.web.config;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRegistration;
|
||||||
|
|
||||||
|
import org.springframework.web.WebApplicationInitializer;
|
||||||
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
|
||||||
|
public class MainWebAppInitializer implements WebApplicationInitializer {
|
||||||
|
|
||||||
|
private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp";
|
||||||
|
private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register and configure all Servlet container components necessary to power the web application.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onStartup(final ServletContext sc) throws ServletException {
|
||||||
|
|
||||||
|
// Create the 'root' Spring application context
|
||||||
|
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||||
|
root.scan("com.baeldung.spring.web.config");
|
||||||
|
// root.getEnvironment().setDefaultProfiles("embedded");
|
||||||
|
|
||||||
|
// Manages the lifecycle of the root application context
|
||||||
|
sc.addListener(new ContextLoaderListener(root));
|
||||||
|
|
||||||
|
// Handles requests into the application
|
||||||
|
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
|
||||||
|
appServlet.setLoadOnStartup(1);
|
||||||
|
|
||||||
|
// final MultipartConfigElement multipartConfigElement = new
|
||||||
|
// MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE,
|
||||||
|
// MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2);
|
||||||
|
//
|
||||||
|
// appServlet.setMultipartConfig(multipartConfigElement);
|
||||||
|
|
||||||
|
final Set<String> mappingConflicts = appServlet.addMapping("/");
|
||||||
|
if (!mappingConflicts.isEmpty()) {
|
||||||
|
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,9 +1,10 @@
|
|||||||
package org.baeldung.spring.web.config;
|
package com.baeldung.spring.web.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
@ -15,14 +16,26 @@ import org.springframework.web.servlet.view.XmlViewResolver;
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@ComponentScan("org.baeldung.web")
|
@ComponentScan("com.baeldung.web")
|
||||||
public class WebConfig extends WebMvcConfigurerAdapter {
|
public class WebConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
public WebConfig() {
|
public WebConfig() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
// @Bean
|
||||||
|
// public StandardServletMultipartResolver multipartResolver() {
|
||||||
|
// return new StandardServletMultipartResolver();
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Bean(name = "multipartResolver")
|
||||||
|
public CommonsMultipartResolver multipartResolver() {
|
||||||
|
|
||||||
|
final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||||
|
multipartResolver.setMaxUploadSize(100000);
|
||||||
|
|
||||||
|
return multipartResolver;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
public void addViewControllers(final ViewControllerRegistry registry) {
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.web;
|
package com.baeldung.web;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.web;
|
package com.baeldung.web;
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
@ -1,9 +1,9 @@
|
|||||||
package org.baeldung.web.controller;
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.baeldung.model.Employee;
|
import com.baeldung.model.Employee;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.ModelMap;
|
import org.springframework.ui.ModelMap;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.ModelMap;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class FileUploadController {
|
||||||
|
|
||||||
|
@RequestMapping(value = "/fileUpload", method = RequestMethod.GET)
|
||||||
|
public String displayForm() {
|
||||||
|
|
||||||
|
return "fileUploadForm";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
|
||||||
|
public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) {
|
||||||
|
|
||||||
|
modelMap.addAttribute("file", file);
|
||||||
|
return "fileUploadView";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
|
||||||
|
public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) {
|
||||||
|
|
||||||
|
modelMap.addAttribute("files", files);
|
||||||
|
return "fileUploadView";
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package org.baeldung.web.controller;
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
import org.baeldung.model.User;
|
import com.baeldung.model.User;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
@ -1,93 +0,0 @@
|
|||||||
package org.baeldung.spring.web.config;
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.baeldung.dialect.CustomDialect;
|
|
||||||
import org.springframework.context.MessageSource;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.Description;
|
|
||||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
||||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
|
||||||
import org.springframework.web.servlet.view.JstlView;
|
|
||||||
import org.thymeleaf.dialect.IDialect;
|
|
||||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
|
||||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
|
||||||
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
|
||||||
|
|
||||||
@EnableWebMvc
|
|
||||||
@Configuration
|
|
||||||
public class ClientWebConfig extends WebMvcConfigurerAdapter {
|
|
||||||
|
|
||||||
public ClientWebConfig() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
|
||||||
super.addViewControllers(registry);
|
|
||||||
|
|
||||||
registry.addViewController("/sample.html");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ViewResolver thymeleafViewResolver() {
|
|
||||||
final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
|
||||||
viewResolver.setTemplateEngine(templateEngine());
|
|
||||||
viewResolver.setOrder(1);
|
|
||||||
return viewResolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ViewResolver viewResolver() {
|
|
||||||
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
|
||||||
bean.setViewClass(JstlView.class);
|
|
||||||
bean.setPrefix("/WEB-INF/view/");
|
|
||||||
bean.setSuffix(".jsp");
|
|
||||||
bean.setOrder(0);
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Description("Thymeleaf template resolver serving HTML 5")
|
|
||||||
public ServletContextTemplateResolver templateResolver() {
|
|
||||||
final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
|
||||||
templateResolver.setPrefix("/WEB-INF/templates/");
|
|
||||||
templateResolver.setSuffix(".html");
|
|
||||||
templateResolver.setTemplateMode("HTML5");
|
|
||||||
return templateResolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Description("Thymeleaf template engine with Spring integration")
|
|
||||||
public SpringTemplateEngine templateEngine() {
|
|
||||||
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
|
||||||
templateEngine.setTemplateResolver(templateResolver());
|
|
||||||
final Set<IDialect> dialects = new HashSet<>();
|
|
||||||
dialects.add(new CustomDialect());
|
|
||||||
templateEngine.setAdditionalDialects(dialects);
|
|
||||||
return templateEngine;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Description("Spring message resolver")
|
|
||||||
public MessageSource messageSource() {
|
|
||||||
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
|
||||||
messageSource.setBasename("messages");
|
|
||||||
return messageSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
|
|
||||||
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
package org.baeldung.spring.web.config;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletRegistration;
|
|
||||||
|
|
||||||
import org.springframework.web.WebApplicationInitializer;
|
|
||||||
import org.springframework.web.context.ContextLoaderListener;
|
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
|
||||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
|
||||||
|
|
||||||
public class MainWebAppInitializer implements WebApplicationInitializer {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register and configure all Servlet container components necessary to power the web application.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onStartup(final ServletContext sc) throws ServletException {
|
|
||||||
System.out.println("MainWebAppInitializer.onStartup()");
|
|
||||||
|
|
||||||
// Create the 'root' Spring application context
|
|
||||||
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
|
||||||
root.scan("org.baeldung.spring.web.config");
|
|
||||||
// root.getEnvironment().setDefaultProfiles("embedded");
|
|
||||||
|
|
||||||
// Manages the lifecycle of the root application context
|
|
||||||
sc.addListener(new ContextLoaderListener(root));
|
|
||||||
|
|
||||||
// Handles requests into the application
|
|
||||||
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
|
|
||||||
appServlet.setLoadOnStartup(1);
|
|
||||||
final Set<String> mappingConflicts = appServlet.addMapping("/");
|
|
||||||
if (!mappingConflicts.isEmpty()) {
|
|
||||||
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -7,8 +7,8 @@
|
|||||||
http://www.springframework.org/schema/aop
|
http://www.springframework.org/schema/aop
|
||||||
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
|
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
|
||||||
|
|
||||||
<bean id="perfomanceMeter" class="org.baeldung.aop.PerformanceAspect"/>
|
<bean id="perfomanceMeter" class="com.baeldung.aop.PerformanceAspect"/>
|
||||||
<bean id="fooDao" class="org.baeldung.dao.FooDao"/>
|
<bean id="fooDao" class="com.baeldung.dao.FooDao"/>
|
||||||
|
|
||||||
<aop:config>
|
<aop:config>
|
||||||
<aop:pointcut id="anyDaoMethod" expression="@target(org.springframework.stereotype.Repository)"/>
|
<aop:pointcut id="anyDaoMethod" expression="@target(org.springframework.stereotype.Repository)"/>
|
@ -0,0 +1,55 @@
|
|||||||
|
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
|
||||||
|
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
|
<title>File Upload Example</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h3>Enter The File to Upload (Single file)</h3>
|
||||||
|
|
||||||
|
<form:form method="POST" action="/spring-mvc-java/uploadFile" enctype="multipart/form-data">
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>Select a file to upload</td>
|
||||||
|
<td><input type="file" name="file" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><input type="submit" value="Submit" /></td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</form:form>
|
||||||
|
|
||||||
|
<br />
|
||||||
|
|
||||||
|
<h3>Enter The Files to Upload (Multiple files)</h3>
|
||||||
|
|
||||||
|
<form:form method="POST" action="/spring-mvc-java/uploadMultiFile" enctype="multipart/form-data">
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>Select a file to upload</td>
|
||||||
|
<td><input type="file" name="files" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Select a file to upload</td>
|
||||||
|
<td><input type="file" name="files" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Select a file to upload</td>
|
||||||
|
<td><input type="file" name="files" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><input type="submit" value="Submit" /></td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</form:form>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
@ -0,0 +1,36 @@
|
|||||||
|
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Spring MVC File Upload</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h2>Submitted File (Single)</h2>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>OriginalFileName :</td>
|
||||||
|
<td>${file.originalFilename}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Type :</td>
|
||||||
|
<td>${file.contentType}</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
<h2>Submitted Files (Multiple)</h2>
|
||||||
|
<table>
|
||||||
|
<c:forEach items="${files}" var="file">
|
||||||
|
<tr>
|
||||||
|
<td>OriginalFileName :</td>
|
||||||
|
<td>${file.originalFilename}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Type :</td>
|
||||||
|
<td>${file.contentType}</td>
|
||||||
|
</tr>
|
||||||
|
</c:forEach>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -16,7 +16,7 @@
|
|||||||
</context-param>
|
</context-param>
|
||||||
<context-param>
|
<context-param>
|
||||||
<param-name>contextConfigLocation</param-name>
|
<param-name>contextConfigLocation</param-name>
|
||||||
<param-value>org.baeldung.spring.web.config</param-value>
|
<param-value>com.baeldung.spring.web.config</param-value>
|
||||||
</context-param>
|
</context-param>
|
||||||
|
|
||||||
<listener>
|
<listener>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package org.baeldung.aop;
|
package com.baeldung.aop;
|
||||||
|
|
||||||
import org.baeldung.config.TestConfig;
|
import com.baeldung.config.TestConfig;
|
||||||
import org.baeldung.dao.FooDao;
|
import com.baeldung.dao.FooDao;
|
||||||
import org.baeldung.model.Foo;
|
import com.baeldung.model.Foo;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
@ -1,7 +1,7 @@
|
|||||||
package org.baeldung.aop;
|
package com.baeldung.aop;
|
||||||
|
|
||||||
import org.baeldung.config.TestConfig;
|
import com.baeldung.config.TestConfig;
|
||||||
import org.baeldung.dao.FooDao;
|
import com.baeldung.dao.FooDao;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
@ -1,9 +1,9 @@
|
|||||||
package org.baeldung.aop;
|
package com.baeldung.aop;
|
||||||
|
|
||||||
import org.baeldung.config.TestConfig;
|
import com.baeldung.config.TestConfig;
|
||||||
import org.baeldung.dao.FooDao;
|
import com.baeldung.dao.FooDao;
|
||||||
import org.baeldung.events.FooCreationEventListener;
|
import com.baeldung.events.FooCreationEventListener;
|
||||||
import org.baeldung.model.Foo;
|
import com.baeldung.model.Foo;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
@ -1,6 +1,6 @@
|
|||||||
package org.baeldung.aop;
|
package com.baeldung.aop;
|
||||||
|
|
||||||
import org.baeldung.dao.FooDao;
|
import com.baeldung.dao.FooDao;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -21,7 +21,7 @@ import static org.junit.Assert.assertThat;
|
|||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("/org/baeldung/aop/beans.xml")
|
@ContextConfiguration("/com/baeldung/aop/beans.xml")
|
||||||
public class AopXmlConfigPerformanceTest {
|
public class AopXmlConfigPerformanceTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
@ -1,11 +1,11 @@
|
|||||||
package org.baeldung.config;
|
package com.baeldung.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(basePackages = { "org.baeldung.dao", "org.baeldung.aop", "org.baeldung.events" })
|
@ComponentScan(basePackages = { "com.baeldung.dao", "com.baeldung.aop", "com.baeldung.events" })
|
||||||
@EnableAspectJAutoProxy
|
@EnableAspectJAutoProxy
|
||||||
public class TestConfig {
|
public class TestConfig {
|
||||||
}
|
}
|
@ -1,6 +1,12 @@
|
|||||||
package com.baeldung.spring;
|
package com.baeldung.spring;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.support.MessageSourceResourceBundle;
|
||||||
|
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
@ -15,7 +21,20 @@ public class ClientWebConfigJava extends WebMvcConfigurerAdapter {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
// API
|
@Bean
|
||||||
|
public MessageSource messageSource() {
|
||||||
|
|
||||||
|
final ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
|
||||||
|
ms.setBasenames("messages");
|
||||||
|
return ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ResourceBundle getBeanResourceBundle() {
|
||||||
|
|
||||||
|
final Locale locale = Locale.getDefault();
|
||||||
|
return new MessageSourceResourceBundle(messageSource(), locale);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
<h3>Welcome, Enter the Person Details</h3>
|
<h3>Welcome, Enter the Person Details</h3>
|
||||||
|
|
||||||
<form:form method="POST" action="addPerson" modelAttribute="person">
|
<form:form method="POST" action="/spring-mvc-xml/addPerson" modelAttribute="person">
|
||||||
|
|
||||||
<form:errors path="*" cssClass="errorbox" element="div" />
|
<form:errors path="*" cssClass="errorbox" element="div" />
|
||||||
|
|
||||||
|
@ -61,6 +61,5 @@
|
|||||||
<td>${person.notes}</td>
|
<td>${person.notes}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -7,6 +7,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au
|
|||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||||
import org.springframework.security.web.session.HttpSessionEventPublisher;
|
import org.springframework.security.web.session.HttpSessionEventPublisher;
|
||||||
|
|
||||||
@ -49,7 +50,12 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
.and()
|
.and()
|
||||||
.rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400)
|
.rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400)
|
||||||
.and()
|
.and()
|
||||||
.sessionManagement().invalidSessionUrl("/invalidSession.html").maximumSessions(2).expiredUrl("/sessionExpired.html");
|
.sessionManagement()
|
||||||
|
.sessionFixation().migrateSession()
|
||||||
|
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
|
||||||
|
.invalidSessionUrl("/invalidSession.html")
|
||||||
|
.maximumSessions(2)
|
||||||
|
.expiredUrl("/sessionExpired.html");
|
||||||
|
|
||||||
// @formatter:on
|
// @formatter:on
|
||||||
}
|
}
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<projectDescription>
|
|
||||||
<name>xstream-introduction</name>
|
|
||||||
<comment>An Introduction To XStream. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
|
|
||||||
<projects/>
|
|
||||||
<buildSpec>
|
|
||||||
<buildCommand>
|
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
|
||||||
</buildCommand>
|
|
||||||
</buildSpec>
|
|
||||||
<natures>
|
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
|
||||||
</natures>
|
|
||||||
</projectDescription>
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.baeldung.initializer;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.XStream;
|
|
||||||
|
|
||||||
public class SimpleXstreamInitializer {
|
|
||||||
|
|
||||||
private static XStream xstreamInstance;
|
|
||||||
|
|
||||||
public static XStream getXstreamInstance() {
|
|
||||||
if (xstreamInstance == null) {
|
|
||||||
synchronized (SimpleXstreamInitializer.class) {
|
|
||||||
if (xstreamInstance == null) {
|
|
||||||
xstreamInstance = new XStream();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return xstreamInstance;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
package com.baeldung.pojo;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
|
||||||
|
|
||||||
@XStreamAlias("AddressDetails")
|
|
||||||
public class AddressDetails {
|
|
||||||
|
|
||||||
private String address;
|
|
||||||
|
|
||||||
private String zipcode;
|
|
||||||
|
|
||||||
private List<ContactDetails> contactDetails;
|
|
||||||
|
|
||||||
public String getZipcode() {
|
|
||||||
return zipcode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setZipcode(String zipcode) {
|
|
||||||
this.zipcode = zipcode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ContactDetails> getContactDetails() {
|
|
||||||
return contactDetails;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setContactDetails(List<ContactDetails> contactDetails) {
|
|
||||||
this.contactDetails = contactDetails;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAddress() {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAddress(String address) {
|
|
||||||
this.address = address;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package com.baeldung.pojo;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
|
||||||
|
|
||||||
@XStreamAlias("ContactDetails")
|
|
||||||
public class ContactDetails {
|
|
||||||
|
|
||||||
private String mobile;
|
|
||||||
|
|
||||||
private String landline;
|
|
||||||
|
|
||||||
public String getMobile() {
|
|
||||||
return mobile;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMobile(String mobile) {
|
|
||||||
this.mobile = mobile;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLandline() {
|
|
||||||
return landline;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLandline(String landline) {
|
|
||||||
this.landline = landline;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
package com.baeldung.pojo;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamOmitField;
|
|
||||||
|
|
||||||
@XStreamAlias("customer")
|
|
||||||
public class Customer {
|
|
||||||
|
|
||||||
//@XStreamOmitField
|
|
||||||
private String firstName;
|
|
||||||
|
|
||||||
private String lastName;
|
|
||||||
|
|
||||||
private Date dob;
|
|
||||||
|
|
||||||
@XStreamImplicit
|
|
||||||
private List<ContactDetails> contactDetailsList;
|
|
||||||
|
|
||||||
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 Date getDob() {
|
|
||||||
return dob;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDob(Date dob) {
|
|
||||||
this.dob = dob;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ContactDetails> getContactDetailsList() {
|
|
||||||
return contactDetailsList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setContactDetailsList(List<ContactDetails> contactDetailsList) {
|
|
||||||
this.contactDetailsList = contactDetailsList;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
package com.baeldung.pojo;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
|
||||||
|
|
||||||
@XStreamAlias("CustomerAddressDetails")
|
|
||||||
public class CustomerAddressDetails {
|
|
||||||
|
|
||||||
private List<AddressDetails> addressDetails;
|
|
||||||
|
|
||||||
private String firstName;
|
|
||||||
|
|
||||||
private String lastName;
|
|
||||||
|
|
||||||
private int age;
|
|
||||||
|
|
||||||
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 int getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(int age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public List<AddressDetails> getAddressDetails() {
|
|
||||||
return addressDetails;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAddressDetails(List<AddressDetails> addressDetails) {
|
|
||||||
this.addressDetails = addressDetails;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package com.baeldung.pojo;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
|
||||||
|
|
||||||
@XStreamAlias("CustomerPortfolio")
|
|
||||||
public class CustomerPortfolio {
|
|
||||||
|
|
||||||
private List<CustomerAddressDetails> customerAddressDetailsList;
|
|
||||||
|
|
||||||
public List<CustomerAddressDetails> getCustomerAddressDetailsList() {
|
|
||||||
return customerAddressDetailsList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCustomerAddressDetailsList(List<CustomerAddressDetails> customerAddressDetailsList) {
|
|
||||||
this.customerAddressDetailsList = customerAddressDetailsList;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
package com.baeldung.utility;
|
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.GregorianCalendar;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.converters.ConversionException;
|
|
||||||
import com.thoughtworks.xstream.converters.Converter;
|
|
||||||
import com.thoughtworks.xstream.converters.MarshallingContext;
|
|
||||||
import com.thoughtworks.xstream.converters.UnmarshallingContext;
|
|
||||||
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
|
|
||||||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
|
||||||
|
|
||||||
public class MyDateConverter implements Converter {
|
|
||||||
|
|
||||||
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canConvert(Class clazz) {
|
|
||||||
return Date.class.isAssignableFrom(clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void marshal(Object value , HierarchicalStreamWriter writer , MarshallingContext arg2) {
|
|
||||||
Date date = (Date) value;
|
|
||||||
writer.setValue(formatter.format(date));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object unmarshal(HierarchicalStreamReader reader , UnmarshallingContext arg1) {
|
|
||||||
GregorianCalendar calendar = new GregorianCalendar();
|
|
||||||
try {
|
|
||||||
calendar.setTime(formatter.parse(reader.getValue()));
|
|
||||||
} catch (ParseException e) {
|
|
||||||
throw new ConversionException(e.getMessage() , e);
|
|
||||||
}
|
|
||||||
return calendar;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package com.baeldung.utility;
|
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import com.baeldung.pojo.Customer;
|
|
||||||
import com.thoughtworks.xstream.converters.SingleValueConverter;
|
|
||||||
|
|
||||||
public class MySingleValueConverter implements SingleValueConverter {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canConvert(Class clazz) {
|
|
||||||
return Customer.class.isAssignableFrom(clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object fromString(String arg0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(Object obj) {
|
|
||||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
|
||||||
Date date = ((Customer) obj).getDob();
|
|
||||||
return ((Customer) obj).getFirstName() + "," + ((Customer) obj).getLastName() + "," + formatter.format(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
package com.baeldung.utility;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.baeldung.pojo.ContactDetails;
|
|
||||||
import com.baeldung.pojo.Customer;
|
|
||||||
|
|
||||||
public class SimpleDataGeneration {
|
|
||||||
|
|
||||||
public static Customer generateData() {
|
|
||||||
Customer customer = new Customer();
|
|
||||||
Calendar cal = Calendar.getInstance();
|
|
||||||
cal.set(1986 , 01 , 14);
|
|
||||||
customer.setDob(cal.getTime());
|
|
||||||
customer.setFirstName("XStream");
|
|
||||||
customer.setLastName("Java");
|
|
||||||
|
|
||||||
List<ContactDetails> contactDetailsList = new ArrayList<ContactDetails>();
|
|
||||||
|
|
||||||
ContactDetails contactDetails1 = new ContactDetails();
|
|
||||||
contactDetails1.setLandline("0124-2460311");
|
|
||||||
contactDetails1.setMobile("6673543265");
|
|
||||||
|
|
||||||
ContactDetails contactDetails2 = new ContactDetails();
|
|
||||||
contactDetails2.setLandline("0120-223312");
|
|
||||||
contactDetails2.setMobile("4676543565");
|
|
||||||
|
|
||||||
contactDetailsList.add(contactDetails1);
|
|
||||||
contactDetailsList.add(contactDetails2);
|
|
||||||
|
|
||||||
customer.setContactDetailsList(contactDetailsList);
|
|
||||||
return customer;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,62 +0,0 @@
|
|||||||
package com.baeldung.utility;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
|
||||||
import com.baeldung.pojo.AddressDetails;
|
|
||||||
import com.baeldung.pojo.ContactDetails;
|
|
||||||
import com.baeldung.pojo.Customer;
|
|
||||||
import com.baeldung.utility.SimpleDataGeneration;
|
|
||||||
import com.thoughtworks.xstream.XStream;
|
|
||||||
|
|
||||||
public class XStreamSimpleXmlTest {
|
|
||||||
|
|
||||||
private Customer customer = null;
|
|
||||||
|
|
||||||
private String dataXml = null;
|
|
||||||
|
|
||||||
private XStream xstream = null;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void dataSetup() {
|
|
||||||
customer = SimpleDataGeneration.generateData();
|
|
||||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
|
||||||
xstream.processAnnotations(Customer.class);
|
|
||||||
xstream.processAnnotations(AddressDetails.class);
|
|
||||||
xstream.processAnnotations(ContactDetails.class);
|
|
||||||
xstream.omitField(Customer.class , "lastName");
|
|
||||||
xstream.registerConverter(new MyDateConverter());
|
|
||||||
// xstream.registerConverter(new MySingleValueConverter());
|
|
||||||
xstream.aliasField("fn" , Customer.class , "firstName");
|
|
||||||
|
|
||||||
dataXml = xstream.toXML(customer);
|
|
||||||
System.out.println(dataXml);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testClassAliasedAnnotation() {
|
|
||||||
Assert.assertNotEquals(-1 , dataXml.indexOf("<customer>"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testFieldAliasedAnnotation() {
|
|
||||||
Assert.assertNotEquals(-1 , dataXml.indexOf("<fn>"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testImplicitCollection() {
|
|
||||||
Assert.assertEquals(-1 , dataXml.indexOf("contactDetailsList"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDateFieldFormating() {
|
|
||||||
Assert.assertEquals("14-02-1986" , dataXml.substring(dataXml.indexOf("<dob>") + 5 , dataXml.indexOf("</dob>")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOmitField() {
|
|
||||||
Assert.assertEquals(-1 , dataXml.indexOf("lastName"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -27,4 +27,18 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.annotation.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@XStreamAlias("customer")
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
@XStreamAlias("fn")
|
||||||
|
private String firstName;
|
||||||
|
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
private Date dob;
|
||||||
|
|
||||||
|
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 Date getDob() {
|
||||||
|
return dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDob(Date dob) {
|
||||||
|
this.dob = dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||||
|
+ ", dob=" + dob + "]";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.annotation.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamOmitField;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
||||||
|
@XStreamAlias("customer")
|
||||||
|
public class CustomerOmitField {
|
||||||
|
|
||||||
|
@XStreamOmitField
|
||||||
|
private String firstName;
|
||||||
|
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
private Date dob;
|
||||||
|
|
||||||
|
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 Date getDob() {
|
||||||
|
return dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDob(Date dob) {
|
||||||
|
this.dob = dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName="
|
||||||
|
+ lastName + ", dob=" + dob + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.complex.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||||
|
|
||||||
|
@XStreamAlias("ContactDetails")
|
||||||
|
public class ContactDetails {
|
||||||
|
|
||||||
|
private String mobile;
|
||||||
|
|
||||||
|
private String landline;
|
||||||
|
|
||||||
|
@XStreamAsAttribute
|
||||||
|
private String contactType;
|
||||||
|
|
||||||
|
public String getMobile() {
|
||||||
|
return mobile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMobile(String mobile) {
|
||||||
|
this.mobile = mobile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLandline() {
|
||||||
|
return landline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLandline(String landline) {
|
||||||
|
this.landline = landline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContactType() {
|
||||||
|
return contactType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContactType(String contactType) {
|
||||||
|
this.contactType = contactType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ContactDetails [mobile=" + mobile + ", landline=" + landline
|
||||||
|
+ ", contactType=" + contactType + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.complex.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XStreamAlias("customer")
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
private Date dob;
|
||||||
|
|
||||||
|
private List<ContactDetails> contactDetailsList;
|
||||||
|
|
||||||
|
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 Date getDob() {
|
||||||
|
return dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDob(Date dob) {
|
||||||
|
this.dob = dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ContactDetails> getContactDetailsList() {
|
||||||
|
return contactDetailsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContactDetailsList(List<ContactDetails> contactDetailsList) {
|
||||||
|
this.contactDetailsList = contactDetailsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||||
|
+ ", dob=" + dob + ", contactDetailsList=" + contactDetailsList
|
||||||
|
+ "]";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.implicit.collection.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||||
|
|
||||||
|
@XStreamAlias("ContactDetails")
|
||||||
|
public class ContactDetails {
|
||||||
|
|
||||||
|
private String mobile;
|
||||||
|
|
||||||
|
private String landline;
|
||||||
|
|
||||||
|
@XStreamAsAttribute
|
||||||
|
private String contactType;
|
||||||
|
|
||||||
|
public String getMobile() {
|
||||||
|
return mobile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMobile(String mobile) {
|
||||||
|
this.mobile = mobile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLandline() {
|
||||||
|
return landline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLandline(String landline) {
|
||||||
|
this.landline = landline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContactType() {
|
||||||
|
return contactType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContactType(String contactType) {
|
||||||
|
this.contactType = contactType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ContactDetails [mobile=" + mobile + ", landline=" + landline
|
||||||
|
+ ", contactType=" + contactType + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.implicit.collection.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XStreamAlias("customer")
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
private Date dob;
|
||||||
|
|
||||||
|
@XStreamImplicit
|
||||||
|
private List<ContactDetails> contactDetailsList;
|
||||||
|
|
||||||
|
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 Date getDob() {
|
||||||
|
return dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDob(Date dob) {
|
||||||
|
this.dob = dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ContactDetails> getContactDetailsList() {
|
||||||
|
return contactDetailsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContactDetailsList(List<ContactDetails> contactDetailsList) {
|
||||||
|
this.contactDetailsList = contactDetailsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||||
|
+ ", dob=" + dob + ", contactDetailsList=" + contactDetailsList
|
||||||
|
+ "]";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.initializer;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
|
||||||
|
public class SimpleXstreamInitializer {
|
||||||
|
|
||||||
|
private XStream xtreamInstance;
|
||||||
|
|
||||||
|
public XStream getXstreamInstance() {
|
||||||
|
if (xtreamInstance == null) {
|
||||||
|
synchronized (SimpleXstreamInitializer.class) {
|
||||||
|
if (xtreamInstance == null) {
|
||||||
|
xtreamInstance = new XStream();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return xtreamInstance;
|
||||||
|
}
|
||||||
|
}
|
40
xstream/src/main/java/com/baeldung/pojo/AddressDetails.java
Normal file
40
xstream/src/main/java/com/baeldung/pojo/AddressDetails.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XStreamAlias("AddressDetails")
|
||||||
|
public class AddressDetails {
|
||||||
|
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
private String zipcode;
|
||||||
|
|
||||||
|
private List<ContactDetails> contactDetails;
|
||||||
|
|
||||||
|
public String getZipcode() {
|
||||||
|
return zipcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZipcode(String zipcode) {
|
||||||
|
this.zipcode = zipcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ContactDetails> getContactDetails() {
|
||||||
|
return contactDetails;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContactDetails(List<ContactDetails> contactDetails) {
|
||||||
|
this.contactDetails = contactDetails;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
28
xstream/src/main/java/com/baeldung/pojo/ContactDetails.java
Normal file
28
xstream/src/main/java/com/baeldung/pojo/ContactDetails.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
|
@XStreamAlias("ContactDetails")
|
||||||
|
public class ContactDetails {
|
||||||
|
|
||||||
|
private String mobile;
|
||||||
|
|
||||||
|
private String landline;
|
||||||
|
|
||||||
|
public String getMobile() {
|
||||||
|
return mobile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMobile(String mobile) {
|
||||||
|
this.mobile = mobile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLandline() {
|
||||||
|
return landline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLandline(String landline) {
|
||||||
|
this.landline = landline;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
57
xstream/src/main/java/com/baeldung/pojo/Customer.java
Normal file
57
xstream/src/main/java/com/baeldung/pojo/Customer.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XStreamAlias("customer")
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
private Date dob;
|
||||||
|
|
||||||
|
@XStreamImplicit
|
||||||
|
private List<ContactDetails> contactDetailsList;
|
||||||
|
|
||||||
|
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 Date getDob() {
|
||||||
|
return dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDob(Date dob) {
|
||||||
|
this.dob = dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ContactDetails> getContactDetailsList() {
|
||||||
|
return contactDetailsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContactDetailsList(List<ContactDetails> contactDetailsList) {
|
||||||
|
this.contactDetailsList = contactDetailsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XStreamAlias("CustomerAddressDetails")
|
||||||
|
public class CustomerAddressDetails {
|
||||||
|
|
||||||
|
private List<AddressDetails> addressDetails;
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
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 int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<AddressDetails> getAddressDetails() {
|
||||||
|
return addressDetails;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddressDetails(List<AddressDetails> addressDetails) {
|
||||||
|
this.addressDetails = addressDetails;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.pojo;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XStreamAlias("CustomerPortfolio")
|
||||||
|
public class CustomerPortfolio {
|
||||||
|
|
||||||
|
private List<CustomerAddressDetails> customerAddressDetailsList;
|
||||||
|
|
||||||
|
public List<CustomerAddressDetails> getCustomerAddressDetailsList() {
|
||||||
|
return customerAddressDetailsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomerAddressDetailsList(List<CustomerAddressDetails> customerAddressDetailsList) {
|
||||||
|
this.customerAddressDetailsList = customerAddressDetailsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.utility;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.converters.ConversionException;
|
||||||
|
import com.thoughtworks.xstream.converters.Converter;
|
||||||
|
import com.thoughtworks.xstream.converters.MarshallingContext;
|
||||||
|
import com.thoughtworks.xstream.converters.UnmarshallingContext;
|
||||||
|
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
|
||||||
|
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
public class MyDateConverter implements Converter {
|
||||||
|
|
||||||
|
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canConvert(Class clazz) {
|
||||||
|
return Date.class.isAssignableFrom(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext arg2) {
|
||||||
|
Date date = (Date) value;
|
||||||
|
writer.setValue(formatter.format(date));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext arg1) {
|
||||||
|
GregorianCalendar calendar = new GregorianCalendar();
|
||||||
|
try {
|
||||||
|
calendar.setTime(formatter.parse(reader.getValue()));
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new ConversionException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return calendar;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.utility;
|
||||||
|
|
||||||
|
import com.baeldung.pojo.Customer;
|
||||||
|
import com.thoughtworks.xstream.converters.SingleValueConverter;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class MySingleValueConverter implements SingleValueConverter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canConvert(Class clazz) {
|
||||||
|
return Customer.class.isAssignableFrom(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object fromString(String arg0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(Object obj) {
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
||||||
|
Date date = ((Customer) obj).getDob();
|
||||||
|
return ((Customer) obj).getFirstName() + "," + ((Customer) obj).getLastName() + "," + formatter.format(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.utility;
|
||||||
|
|
||||||
|
import com.baeldung.pojo.ContactDetails;
|
||||||
|
import com.baeldung.pojo.Customer;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SimpleDataGeneration {
|
||||||
|
|
||||||
|
public static Customer generateData() {
|
||||||
|
Customer customer = new Customer();
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.set(1986, 01, 14);
|
||||||
|
customer.setDob(cal.getTime());
|
||||||
|
customer.setFirstName("XStream");
|
||||||
|
customer.setLastName("Java");
|
||||||
|
|
||||||
|
List<ContactDetails> contactDetailsList = new ArrayList<ContactDetails>();
|
||||||
|
|
||||||
|
ContactDetails contactDetails1 = new ContactDetails();
|
||||||
|
contactDetails1.setLandline("0124-2460311");
|
||||||
|
contactDetails1.setMobile("6673543265");
|
||||||
|
|
||||||
|
ContactDetails contactDetails2 = new ContactDetails();
|
||||||
|
contactDetails2.setLandline("0120-223312");
|
||||||
|
contactDetails2.setMobile("4676543565");
|
||||||
|
|
||||||
|
contactDetailsList.add(contactDetails1);
|
||||||
|
contactDetailsList.add(contactDetails2);
|
||||||
|
|
||||||
|
customer.setContactDetailsList(contactDetailsList);
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.pojo.test;
|
||||||
|
|
||||||
|
import com.baeldung.complex.pojo.Customer;
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ComplexXmlToObjectAnnotationTest {
|
||||||
|
|
||||||
|
private XStream xstream = null;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
xstream.processAnnotations(Customer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromFile() {
|
||||||
|
try {
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile());
|
||||||
|
Customer customer = (Customer) xstream.fromXML(reader);
|
||||||
|
Assert.assertNotNull(customer);
|
||||||
|
Assert.assertNotNull(customer.getContactDetailsList());
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.pojo.test;
|
||||||
|
|
||||||
|
import com.baeldung.complex.pojo.ContactDetails;
|
||||||
|
import com.baeldung.complex.pojo.Customer;
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ComplexXmlToObjectAttributeCollectionTest {
|
||||||
|
|
||||||
|
private XStream xstream = null;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
xstream.processAnnotations(Customer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromFile() {
|
||||||
|
try {
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile());
|
||||||
|
Customer customer = (Customer) xstream.fromXML(reader);
|
||||||
|
Assert.assertNotNull(customer);
|
||||||
|
Assert.assertNotNull(customer.getContactDetailsList());
|
||||||
|
for (ContactDetails contactDetails : customer.getContactDetailsList()) {
|
||||||
|
Assert.assertNotNull(contactDetails.getContactType());
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.pojo.test;
|
||||||
|
|
||||||
|
import com.baeldung.implicit.collection.pojo.Customer;
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ComplexXmlToObjectCollectionTest {
|
||||||
|
|
||||||
|
private XStream xstream = null;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
xstream.processAnnotations(Customer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromFile() {
|
||||||
|
try {
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile());
|
||||||
|
Customer customer = (Customer) xstream.fromXML(reader);
|
||||||
|
Assert.assertNotNull(customer);
|
||||||
|
Assert.assertNotNull(customer.getContactDetailsList());
|
||||||
|
//System.out.println(customer);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.pojo.test;
|
||||||
|
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.baeldung.pojo.Customer;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class XmlToObjectAliasTest {
|
||||||
|
|
||||||
|
private XStream xstream = null;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
xstream.alias("customer", Customer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromFile() {
|
||||||
|
try {
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile());
|
||||||
|
Customer customer = (Customer) xstream.fromXML(reader);
|
||||||
|
Assert.assertNotNull(customer);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.pojo.test;
|
||||||
|
|
||||||
|
import com.baeldung.annotation.pojo.Customer;
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class XmlToObjectAnnotationTest {
|
||||||
|
|
||||||
|
private XStream xstream = null;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
xstream.processAnnotations(Customer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromFile() {
|
||||||
|
try {
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile());
|
||||||
|
Customer customer = (Customer) xstream.fromXML(reader);
|
||||||
|
Assert.assertNotNull(customer);
|
||||||
|
Assert.assertNotNull(customer.getFirstName());
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.pojo.test;
|
||||||
|
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.baeldung.pojo.Customer;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class XmlToObjectFieldAliasTest {
|
||||||
|
|
||||||
|
private XStream xstream = null;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
xstream.alias("customer", Customer.class);
|
||||||
|
xstream.aliasField("fn", Customer.class, "firstName");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromFile() {
|
||||||
|
try {
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile());
|
||||||
|
Customer customer = (Customer) xstream.fromXML(reader);
|
||||||
|
Assert.assertNotNull(customer);
|
||||||
|
Assert.assertNotNull(customer.getFirstName());
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.pojo.test;
|
||||||
|
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.baeldung.pojo.Customer;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class XmlToObjectIgnoreFieldsTest {
|
||||||
|
|
||||||
|
private XStream xstream = null;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
xstream.alias("customer", Customer.class);
|
||||||
|
xstream.ignoreUnknownElements();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromFile() {
|
||||||
|
try {
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile());
|
||||||
|
Customer customer = (Customer) xstream.fromXML(reader);
|
||||||
|
Assert.assertNotNull(customer);
|
||||||
|
//System.out.println(customer);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.pojo.test;
|
||||||
|
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.baeldung.pojo.Customer;
|
||||||
|
import com.baeldung.utility.SimpleDataGeneration;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class XmlToObjectTest {
|
||||||
|
|
||||||
|
private XStream xstream = null;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromFile() {
|
||||||
|
try {
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile());
|
||||||
|
Customer customer = (Customer) xstream.fromXML(reader);
|
||||||
|
Assert.assertNotNull(customer);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertXmlToObjectFromString() {
|
||||||
|
Customer customer = SimpleDataGeneration.generateData();
|
||||||
|
String dataXml = xstream.toXML(customer);
|
||||||
|
Customer convertedCustomer = (Customer) xstream.fromXML(dataXml);
|
||||||
|
Assert.assertNotNull(convertedCustomer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.utility;
|
||||||
|
|
||||||
|
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||||
|
import com.baeldung.pojo.AddressDetails;
|
||||||
|
import com.baeldung.pojo.ContactDetails;
|
||||||
|
import com.baeldung.pojo.Customer;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class XStreamSimpleXmlTest {
|
||||||
|
|
||||||
|
private Customer customer;
|
||||||
|
private String dataXml;
|
||||||
|
private XStream xstream;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void dataSetup() {
|
||||||
|
customer = SimpleDataGeneration.generateData();
|
||||||
|
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||||
|
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||||
|
xstream.processAnnotations(Customer.class);
|
||||||
|
xstream.processAnnotations(AddressDetails.class);
|
||||||
|
xstream.processAnnotations(ContactDetails.class);
|
||||||
|
xstream.omitField(Customer.class, "lastName");
|
||||||
|
xstream.registerConverter(new MyDateConverter());
|
||||||
|
// xstream.registerConverter(new MySingleValueConverter());
|
||||||
|
xstream.aliasField("fn", Customer.class, "firstName");
|
||||||
|
dataXml = xstream.toXML(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClassAliasedAnnotation() {
|
||||||
|
Assert.assertNotEquals(-1, dataXml.indexOf("<customer>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFieldAliasedAnnotation() {
|
||||||
|
Assert.assertNotEquals(-1, dataXml.indexOf("<fn>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testImplicitCollection() {
|
||||||
|
Assert.assertEquals(-1, dataXml.indexOf("contactDetailsList"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDateFieldFormating() {
|
||||||
|
Assert.assertEquals("14-02-1986", dataXml.substring(dataXml.indexOf("<dob>") + 5, dataXml.indexOf("</dob>")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOmitField() {
|
||||||
|
Assert.assertEquals(-1, dataXml.indexOf("lastName"));
|
||||||
|
}
|
||||||
|
}
|
15
xstream/src/test/resources/data-file-alias-field-complex.xml
Normal file
15
xstream/src/test/resources/data-file-alias-field-complex.xml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<customer>
|
||||||
|
<firstName>XStream</firstName>
|
||||||
|
<lastName>Java</lastName>
|
||||||
|
<dob>1986-02-14 04:14:05.874 UTC</dob>
|
||||||
|
<contactDetailsList>
|
||||||
|
<ContactDetails contactType="Home">
|
||||||
|
<mobile>6673543265</mobile>
|
||||||
|
<landline>0124-2460311</landline>
|
||||||
|
</ContactDetails>
|
||||||
|
<ContactDetails contactType="Office">
|
||||||
|
<mobile>4676543565</mobile>
|
||||||
|
<landline>0120-223312</landline>
|
||||||
|
</ContactDetails>
|
||||||
|
</contactDetailsList>
|
||||||
|
</customer>
|
5
xstream/src/test/resources/data-file-alias-field.xml
Normal file
5
xstream/src/test/resources/data-file-alias-field.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<customer>
|
||||||
|
<fn>XStream</fn>
|
||||||
|
<lastName>Java</lastName>
|
||||||
|
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||||
|
</customer>
|
@ -0,0 +1,13 @@
|
|||||||
|
<customer>
|
||||||
|
<firstName>XStream</firstName>
|
||||||
|
<lastName>Java</lastName>
|
||||||
|
<dob>1986-02-14 04:14:20.541 UTC</dob>
|
||||||
|
<ContactDetails contactType="Home">
|
||||||
|
<mobile>6673543265</mobile>
|
||||||
|
<landline>0124-2460311</landline>
|
||||||
|
</ContactDetails>
|
||||||
|
<ContactDetails contactType="Office">
|
||||||
|
<mobile>4676543565</mobile>
|
||||||
|
<landline>0120-223312</landline>
|
||||||
|
</ContactDetails>
|
||||||
|
</customer>
|
5
xstream/src/test/resources/data-file-alias.xml
Normal file
5
xstream/src/test/resources/data-file-alias.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<customer>
|
||||||
|
<firstName>XStream</firstName>
|
||||||
|
<lastName>Java</lastName>
|
||||||
|
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||||
|
</customer>
|
6
xstream/src/test/resources/data-file-ignore-field.xml
Normal file
6
xstream/src/test/resources/data-file-ignore-field.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<customer>
|
||||||
|
<firstName>XStream</firstName>
|
||||||
|
<lastName>Java</lastName>
|
||||||
|
<dob>1986-02-14 04:14:20.541 UTC</dob>
|
||||||
|
<fullName>XStream Java</fullName>
|
||||||
|
</customer>
|
5
xstream/src/test/resources/data-file.xml
Normal file
5
xstream/src/test/resources/data-file.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<com.baeldung.pojo.Customer>
|
||||||
|
<firstName>XStream</firstName>
|
||||||
|
<lastName>Java</lastName>
|
||||||
|
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||||
|
</com.baeldung.pojo.Customer>
|
Loading…
x
Reference in New Issue
Block a user