Skip to content

Commit

Permalink
Exemplo do artigo.
Browse files Browse the repository at this point in the history
  • Loading branch information
xandreafonso committed Sep 30, 2016
0 parents commit 74886ab
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.DS_Store
.classpath
.project
.settings/
target/
tmp
local.properties
20 changes: 20 additions & 0 deletions README.md
@@ -0,0 +1,20 @@
# algaworks-jsf-facesmessage-pagredirecionamento

Código referente ao artigo escrito para AlgaWorks

Saiba mais em:

http://blog.algaworks.com

### Rodando o projeto

Use:

``` shell
$ mvn jetty:run
```

Acesse:

[http://localhost:8080/rachinha/jogadores](http://localhost:8080/rachinha/jogadores)

1 change: 1 addition & 0 deletions maven-generate.sh
@@ -0,0 +1 @@
mvn archetype:generate -DgroupId=com.algaworks.exemplos -DartifactId=algaworks-java-serialVersionUID -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
39 changes: 39 additions & 0 deletions pom.xml
@@ -0,0 +1,39 @@
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>com.algaworks.exemplos</groupId>
<artifactId>algaworks-java-serialVersionUID</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

<name>algaworks-java-serialVersionUID</name>

<url>http://algaworks.com</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,5 @@
package com.algaworks.exemplos.serialversionuid.ex1;

public class Clube implements java.io.Serializable {

}
@@ -0,0 +1,10 @@
package com.algaworks.exemplos.serialversionuid.ex2;

public class Clube implements java.io.Serializable {


String nome;
//int titulos;


}
@@ -0,0 +1,38 @@
package com.algaworks.exemplos.serialversionuid.ex3;

import java.time.LocalDate;

public class Clube implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private String nome;

private int titulos;

private LocalDate nascimento;

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public int getTitulos() {
return titulos;
}

public void setTitulos(int titulos) {
this.titulos = titulos;
}

public LocalDate getNascimento() {
return nascimento;
}

public void setNascimento(LocalDate nascimento) {
this.nascimento = nascimento;
}
}
@@ -0,0 +1,17 @@
package com.algaworks.exemplos.serialversionuid.ex3;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class Desserializador {

public static void main(String... args) throws Exception {
FileInputStream fis = new FileInputStream("/tmp/saopaulo.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Clube clube = (Clube) ois.readObject();
ois.close();

System.out.println("Pronto! Objeto descerializado.");
System.out.println("Nome: " + clube.getNome());
}
}
@@ -0,0 +1,22 @@
package com.algaworks.exemplos.serialversionuid.ex3;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDate;
import java.time.Month;

public class Serializador {

public static void main(String... args) throws Exception {
Clube oMelhorClube = new Clube();
oMelhorClube.setNome("São Paulo Futebol Clube");
oMelhorClube.setTitulos(2147483647);// por 1 não tenho que declarar long
oMelhorClube.setNascimento(LocalDate.of(1930, Month.JANUARY, 27));

FileOutputStream fos = new FileOutputStream("/tmp/saopaulo.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(oMelhorClube);
oos.close();
System.out.println("Pronto! Objeto serializado.");
}
}
@@ -0,0 +1,67 @@
package com.algaworks.exemplos.serialversionuid.ex4;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDate;

public class Clube implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private String nome;

private int titulos;

private LocalDate nascimento;

private transient String hino;

private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();

out.writeObject(hino);

System.out.println("Serializacao customizada.");
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();

hino = (String) in.readObject();

System.out.println("Desserializacao customizada.");
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public int getTitulos() {
return titulos;
}

public void setTitulos(int titulos) {
this.titulos = titulos;
}

public LocalDate getNascimento() {
return nascimento;
}

public void setNascimento(LocalDate nascimento) {
this.nascimento = nascimento;
}

public String getHino() {
return hino;
}

public void setHino(String hino) {
this.hino = hino;
}
}
@@ -0,0 +1,17 @@
package com.algaworks.exemplos.serialversionuid.ex4;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class Desserializador {

public static void main(String... args) throws Exception {
FileInputStream fis = new FileInputStream("/tmp/saopaulo.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Clube clube = (Clube) ois.readObject();
ois.close();

System.out.println("Pronto! Objeto descerializado.");
System.out.println("Nome: " + clube.getNome() + ", Hino: " + clube.getHino());
}
}
@@ -0,0 +1,23 @@
package com.algaworks.exemplos.serialversionuid.ex4;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDate;
import java.time.Month;

public class Serializador {

public static void main(String... args) throws Exception {
Clube oMelhorClube = new Clube();
oMelhorClube.setNome("São Paulo Futebol Clube");
oMelhorClube.setTitulos(2147483647);// por 1 não tenho que declarar long
oMelhorClube.setNascimento(LocalDate.of(1930, Month.JANUARY, 27));
oMelhorClube.setHino("Salve o tricolor paulista... Amado clube brasileiro.");

FileOutputStream fos = new FileOutputStream("/tmp/saopaulo.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(oMelhorClube);
oos.close();
System.out.println("Pronto! Objeto serializado.");
}
}

0 comments on commit 74886ab

Please sign in to comment.