Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions json-customize/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>struts-examples</artifactId>
Expand All @@ -15,6 +16,10 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<flexjson.version>3.3</flexjson.version>

<junit.version>4.13.2</junit.version>
<json-unit-assertj.version>5.1.2</json-unit-assertj.version>
</properties>

<dependencies>
Expand All @@ -34,13 +39,47 @@
<dependency>
<groupId>net.sf.flexjson</groupId>
<artifactId>flexjson</artifactId>
<version>3.3</version>
<version>${flexjson.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-web.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>${json-unit-assertj.version}</version>
<scope>test</scope>
</dependency>

<!-- Required by json-unit-assertj -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>${struts2.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

Expand All @@ -63,4 +102,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
10 changes: 7 additions & 3 deletions json-customize/src/main/java/org/demo/ExcludeTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import flexjson.transformer.AbstractTransformer;

public class ExcludeTransformer extends AbstractTransformer {
public void transform(Object o) {
return;
}
@Override
public Boolean isInline() {
return true;
}

@Override
public void transform(Object o) {}
}
7 changes: 3 additions & 4 deletions json-customize/src/main/java/org/demo/FlexJSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@

import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONWriter;

import java.util.Collection;
import java.util.Date;
import java.util.regex.Pattern;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONWriter;

/**
* Customized JSONWriter using Flexjson
Expand All @@ -41,7 +40,7 @@ public String write(Object object) throws JSONException {
public String write(Object object, Collection<Pattern> excludeProperties, Collection<Pattern> includeProperties,
boolean excludeNullProperties) throws JSONException {

JSONSerializer serializer = new JSONSerializer();
JSONSerializer serializer = new JSONSerializer().exclude("*.class");
if (excludeProperties != null) {
for (Pattern p : excludeProperties) {
serializer = serializer.exclude(p.pattern());
Expand Down
89 changes: 43 additions & 46 deletions json-customize/src/main/java/org/demo/ProduceAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,62 @@

package org.demo;

import org.apache.struts2.ActionSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ActionSupport;

/**
* <code>Set welcome message.</code>
*/
/** <code>Set welcome message.</code> */
public class ProduceAction extends ActionSupport {

private User user;
private User user;

public String execute() throws Exception {
user = new User();
public String execute() throws Exception {
user = new User();

user.setName("William Shakespeare");
user.setName("William Shakespeare");

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
user.setBirthday(formatter.parse("26-Apr-1564"));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
user.setBirthday(formatter.parse("26-04-1564"));

List<Phone> phoneNumbers = new ArrayList<Phone>();
Phone phone = new Phone();
phone.setName("cell");
phone.setNumber("555-123-4567");
phoneNumbers.add(phone);
phone = new Phone();
phone.setName("home");
phone.setNumber("555-987-6543");
phoneNumbers.add(phone);
phone = new Phone();
phone.setName("work");
phone.setNumber("555-678-3542");
phoneNumbers.add(phone);
user.setPhoneNumbers(phoneNumbers);
List<Phone> phoneNumbers = new ArrayList<>();
Phone phone = new Phone();
phone.setName("cell");
phone.setNumber("555-123-4567");
phoneNumbers.add(phone);
phone = new Phone();
phone.setName("home");
phone.setNumber("555-987-6543");
phoneNumbers.add(phone);
phone = new Phone();
phone.setName("work");
phone.setNumber("555-678-3542");
phoneNumbers.add(phone);
user.setPhoneNumbers(phoneNumbers);

List<Address> addresses = new ArrayList<Address>();
Address address = new Address();
address.setName("home");
address.setCity("Stratford-upon-Avon");
address.setStreet("Henley");
List<Zipcode> zipcodes = new ArrayList<Zipcode>();
Zipcode zipcode = new Zipcode();
zipcode.setCode("CV37");
zipcodes.add(zipcode);
address.setZipcodes(zipcodes);
addresses.add(address);
user.setAddresses(addresses);
List<Address> addresses = new ArrayList<>();
Address address = new Address();
address.setName("home");
address.setCity("Stratford-upon-Avon");
address.setStreet("Henley");
List<Zipcode> zipcodes = new ArrayList<>();
Zipcode zipcode = new Zipcode();
zipcode.setCode("CV37");
zipcodes.add(zipcode);
address.setZipcodes(zipcodes);
addresses.add(address);
user.setAddresses(addresses);

user.setLogin("WillShak");
user.setHashedPassword("9e107d9d372bb6826bd81d3542a419d6");
user.setLastLogin(new Date());
user.setPassword("will123shak456");
user.setLogin("WillShak");
user.setHashedPassword("9e107d9d372bb6826bd81d3542a419d6");
user.setLastLogin(new Date());
user.setPassword("will123shak456");

return SUCCESS;
}
return SUCCESS;
}

public User getUser() {
return user;
}
public User getUser() {
return user;
}
}
4 changes: 2 additions & 2 deletions json-customize/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"https://struts.apache.org/dtds/struts-2.5.dtd">
"-//Apache Software Foundation//DTD Struts Configuration 6.0//EN"
"http://struts.apache.org/dtds/struts-6.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
Expand Down
11 changes: 6 additions & 5 deletions json-customize/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>

<web-app id="WebApp_ID" version="6.0"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
<display-name>Struts JSON Customize Example</display-name>

<filter>
<filter-name>struts2</filter-name>
Expand Down
48 changes: 48 additions & 0 deletions json-customize/src/test/java/org/demo/ProduceActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.demo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;

import jakarta.servlet.ServletException;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.junit.StrutsJUnit4TestCase;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProduceActionTest extends StrutsJUnit4TestCase<ProduceAction> {
private static final Logger log = LoggerFactory.getLogger(ProduceActionTest.class);

@Test
public void getActionMapping() {
ActionMapping mapping = getActionMapping("/produce.action");
assertNotNull(mapping);
assertEquals("/", mapping.getNamespace());
assertEquals("produce", mapping.getName());
}

@Test
public void executeAction() throws ServletException, UnsupportedEncodingException {
String output = executeAction("/produce.action");
log.info("output: {}", output);

assertNotNull(output);

assertThatJson(output).node("nickname").isAbsent();

assertThatJson(output).node("name").isEqualTo("William Shakespeare");
assertThatJson(output).node("username").isEqualTo("WillShak");
assertThatJson(output).node("password").isEqualTo("******");
assertThatJson(output).node("birthday").isEqualTo("04/26/1564");

assertThatJson(output).node("addresses[0].city").isEqualTo("Stratford-upon-Avon");
assertThatJson(output).node("addresses[0].name").isEqualTo("home");
assertThatJson(output).node("addresses[0].street").isEqualTo("Henley");

assertThatJson(output).node("addresses").isArray().hasSize(1);

assertThatJson(output).node("lastLogin").isString().matches("\\d{2}/\\d{2}/\\d{4}");
}
}
Loading