Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
8881fdcaf4
|
@ -0,0 +1 @@
|
|||
target/
|
|
@ -8,18 +8,42 @@
|
|||
<artifactId>apache-cxf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<properties>
|
||||
<cxf.version>3.1.6</cxf.version>
|
||||
<spring.version>4.3.1.RELEASE</spring.version>
|
||||
<surefire.version>2.19.1</surefire.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-transports-http-jetty</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
@ -33,6 +57,7 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
|
@ -41,16 +66,16 @@
|
|||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<version>1.5.0</version>
|
||||
<version>1.4.19</version>
|
||||
<configuration>
|
||||
<container>
|
||||
<containerId>jetty9x</containerId>
|
||||
<containerId>tomcat8x</containerId>
|
||||
<type>embedded</type>
|
||||
</container>
|
||||
<configuration>
|
||||
<properties>
|
||||
<cargo.hostname>localhost</cargo.hostname>
|
||||
<cargo.servlet.port>8080</cargo.servlet.port>
|
||||
<cargo.servlet.port>8081</cargo.servlet.port>
|
||||
</properties>
|
||||
</configuration>
|
||||
</configuration>
|
||||
|
@ -71,6 +96,7 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
|
@ -91,27 +117,13 @@
|
|||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
</profiles>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-transports-http-jetty</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<cxf.version>3.1.6</cxf.version>
|
||||
<spring.version>4.3.1.RELEASE</spring.version>
|
||||
<surefire.version>2.19.1</surefire.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.cxf.spring;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.apache.cxf.transport.servlet.CXFServlet;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
public class AppInitializer implements WebApplicationInitializer {
|
||||
@Override
|
||||
public void onStartup(ServletContext container) {
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
context.register(ServiceConfiguration.class);
|
||||
container.addListener(new ContextLoaderListener(context));
|
||||
|
||||
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new CXFServlet());
|
||||
dispatcher.addMapping("/services/*");
|
||||
}
|
||||
}
|
|
@ -5,5 +5,6 @@ import javax.jws.WebService;
|
|||
@WebService
|
||||
public interface Baeldung {
|
||||
String hello(String name);
|
||||
|
||||
String register(Student student);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.cxf.spring;
|
||||
|
||||
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ClientConfiguration {
|
||||
@Bean(name = "client")
|
||||
public Object generateProxy() {
|
||||
return proxyFactoryBean().create();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JaxWsProxyFactoryBean proxyFactoryBean() {
|
||||
JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
|
||||
proxyFactory.setServiceClass(Baeldung.class);
|
||||
proxyFactory.setAddress("http://localhost:8081/services/baeldung");
|
||||
return proxyFactory;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.cxf.spring;
|
||||
|
||||
import javax.xml.ws.Endpoint;
|
||||
|
||||
import org.apache.cxf.Bus;
|
||||
import org.apache.cxf.bus.spring.SpringBus;
|
||||
import org.apache.cxf.jaxws.EndpointImpl;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ServiceConfiguration {
|
||||
@Bean(name = Bus.DEFAULT_BUS_ID)
|
||||
public SpringBus springBus() {
|
||||
return new SpringBus();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Endpoint endpoint() {
|
||||
EndpointImpl endpoint = new EndpointImpl(springBus(), new BaeldungImpl());
|
||||
endpoint.publish("http://localhost:8081/services/baeldung");
|
||||
return endpoint;
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
|
||||
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
|
||||
<bean id="client" factory-bean="clientFactory" factory-method="create" />
|
||||
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
|
||||
<property name="serviceClass" value="com.baeldung.cxf.spring.Baeldung" />
|
||||
<property name="address" value="http://localhost:8080/services/baeldung" />
|
||||
</bean>
|
||||
</beans>
|
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
|
||||
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
|
||||
<jaxws:endpoint id="baeldung"
|
||||
implementor="com.baeldung.cxf.spring.BaeldungImpl" address="http://localhost:8080/services/baeldung" />
|
||||
</beans>
|
|
@ -1,14 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
version="3.0"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
|
||||
<servlet>
|
||||
<servlet-name>cxf</servlet-name>
|
||||
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>cxf</servlet-name>
|
||||
<url-pattern>/services/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
</web-app>
|
|
@ -3,15 +3,12 @@ package com.baeldung.cxf.spring;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class StudentTest {
|
||||
private ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "client-beans.xml" });
|
||||
private Baeldung baeldungProxy;
|
||||
|
||||
{
|
||||
baeldungProxy = (Baeldung) context.getBean("client");
|
||||
}
|
||||
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
|
||||
private Baeldung baeldungProxy = (Baeldung) context.getBean("client");
|
||||
|
||||
@Test
|
||||
public void whenUsingHelloMethod_thenCorrect() {
|
||||
|
@ -25,7 +22,7 @@ public class StudentTest {
|
|||
Student student2 = new Student("Eve");
|
||||
String student1Response = baeldungProxy.register(student1);
|
||||
String student2Response = baeldungProxy.register(student2);
|
||||
|
||||
|
||||
assertEquals("Adam is registered student number 1", student1Response);
|
||||
assertEquals("Eve is registered student number 2", student2Response);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>
|
||||
|
||||
|
@ -9,7 +8,7 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<name>Resource vs Inject vs Autowired</name>
|
||||
<name>dependency-injection</name>
|
||||
<description>Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely
|
||||
Resource, Inject, and Autowired
|
||||
</description>
|
||||
|
@ -54,6 +53,7 @@
|
|||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
@ -71,13 +71,29 @@
|
|||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>java.net</id>
|
||||
<url>https://maven.java.net/content/repositories/releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -7,9 +7,19 @@ import java.util.List;
|
|||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class CustomListDeserializer extends JsonDeserializer<List<ItemWithSerializer>> {
|
||||
public class CustomListDeserializer extends StdDeserializer<List<ItemWithSerializer>> {
|
||||
|
||||
private static final long serialVersionUID = 1095767961632979804L;
|
||||
|
||||
public CustomListDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomListDeserializer(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemWithSerializer> deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException {
|
||||
|
|
|
@ -6,11 +6,20 @@ import java.util.List;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomListSerializer extends JsonSerializer<List<ItemWithSerializer>> {
|
||||
public class CustomListSerializer extends StdSerializer<List<ItemWithSerializer>> {
|
||||
|
||||
private static final long serialVersionUID = 3698763098000900856L;
|
||||
|
||||
public CustomListSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomListSerializer(final Class<List<ItemWithSerializer>> t) {
|
||||
super(t);
|
||||
}
|
||||
@Override
|
||||
public void serialize(final List<ItemWithSerializer> items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
final List<Integer> ids = new ArrayList<Integer>();
|
||||
|
|
|
@ -8,12 +8,21 @@ import java.util.Date;
|
|||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class CustomDateDeserializer extends JsonDeserializer<Date> {
|
||||
public class CustomDateDeserializer extends StdDeserializer<Date> {
|
||||
|
||||
private static final long serialVersionUID = -5451717385630622729L;
|
||||
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
|
||||
|
||||
public CustomDateDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomDateDeserializer(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException {
|
||||
final String date = jsonparser.getText();
|
||||
|
|
|
@ -6,13 +6,22 @@ import java.util.Date;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomDateSerializer extends JsonSerializer<Date> {
|
||||
public class CustomDateSerializer extends StdSerializer<Date> {
|
||||
|
||||
private static final long serialVersionUID = -2894356342227378312L;
|
||||
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
|
||||
|
||||
public CustomDateSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomDateSerializer(final Class<Date> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final Date value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
|
||||
gen.writeString(formatter.format(value));
|
||||
|
|
|
@ -8,10 +8,20 @@ import org.joda.time.format.DateTimeFormatter;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomDateTimeSerializer extends JsonSerializer<DateTime> {
|
||||
public class CustomDateTimeSerializer extends StdSerializer<DateTime> {
|
||||
|
||||
private static final long serialVersionUID = -3927232057990121460L;
|
||||
|
||||
public CustomDateTimeSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomDateTimeSerializer(final Class<DateTime> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
|
||||
|
||||
|
|
|
@ -6,13 +6,23 @@ import java.time.format.DateTimeFormatter;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
|
||||
public class CustomLocalDateTimeSerializer extends StdSerializer<LocalDateTime> {
|
||||
|
||||
|
||||
private static final long serialVersionUID = -7449444168934819290L;
|
||||
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||
|
||||
public CustomLocalDateTimeSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomLocalDateTimeSerializer(final Class<LocalDateTime> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final LocalDateTime value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
|
||||
gen.writeString(formatter.format(value));
|
||||
|
|
|
@ -2,17 +2,26 @@ package com.baeldung.jackson.deserialization;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.dtos.User;
|
||||
import com.baeldung.jackson.dtos.Item;
|
||||
|
||||
import com.baeldung.jackson.dtos.User;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.fasterxml.jackson.databind.node.IntNode;
|
||||
|
||||
public class ItemDeserializer extends JsonDeserializer<Item> {
|
||||
public class ItemDeserializer extends StdDeserializer<Item> {
|
||||
|
||||
private static final long serialVersionUID = 1883547683050039861L;
|
||||
|
||||
public ItemDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ItemDeserializer(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
/**
|
||||
* {"id":1,"itemNr":"theItem","owner":2}
|
||||
|
|
|
@ -4,15 +4,24 @@ import java.io.IOException;
|
|||
|
||||
import com.baeldung.jackson.dtos.ItemWithSerializer;
|
||||
import com.baeldung.jackson.dtos.User;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.fasterxml.jackson.databind.node.IntNode;
|
||||
|
||||
public class ItemDeserializerOnClass extends JsonDeserializer<ItemWithSerializer> {
|
||||
public class ItemDeserializerOnClass extends StdDeserializer<ItemWithSerializer> {
|
||||
|
||||
private static final long serialVersionUID = 5579141241817332594L;
|
||||
|
||||
public ItemDeserializerOnClass() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ItemDeserializerOnClass(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
/**
|
||||
* {"id":1,"itemNr":"theItem","owner":2}
|
||||
|
|
|
@ -4,10 +4,20 @@ import java.io.IOException;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class TypeSerializer extends JsonSerializer<TypeEnumWithCustomSerializer> {
|
||||
public class TypeSerializer extends StdSerializer<TypeEnumWithCustomSerializer> {
|
||||
|
||||
private static final long serialVersionUID = -7650668914169390772L;
|
||||
|
||||
public TypeSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public TypeSerializer(final Class<TypeEnumWithCustomSerializer> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final TypeEnumWithCustomSerializer value, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
|
|
|
@ -3,13 +3,22 @@ package com.baeldung.jackson.serialization;
|
|||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.dtos.Item;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class ItemSerializer extends JsonSerializer<Item> {
|
||||
public class ItemSerializer extends StdSerializer<Item> {
|
||||
|
||||
private static final long serialVersionUID = 6739170890621978901L;
|
||||
|
||||
public ItemSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ItemSerializer(final Class<Item> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
|
|
|
@ -3,13 +3,22 @@ package com.baeldung.jackson.serialization;
|
|||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.dtos.ItemWithSerializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class ItemSerializerOnClass extends JsonSerializer<ItemWithSerializer> {
|
||||
public class ItemSerializerOnClass extends StdSerializer<ItemWithSerializer> {
|
||||
|
||||
private static final long serialVersionUID = -1760959597313610409L;
|
||||
|
||||
public ItemSerializerOnClass() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ItemSerializerOnClass(final Class<ItemWithSerializer> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void serialize(final ItemWithSerializer value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
|
|
|
@ -4,10 +4,20 @@ import java.io.IOException;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class MyDtoNullKeySerializer extends JsonSerializer<Object> {
|
||||
public class MyDtoNullKeySerializer extends StdSerializer<Object> {
|
||||
|
||||
private static final long serialVersionUID = -4478531309177369056L;
|
||||
|
||||
public MyDtoNullKeySerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public MyDtoNullKeySerializer(final Class<Object> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
|
|
|
@ -6,10 +6,19 @@ import com.fasterxml.jackson.core.JsonParser;
|
|||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.ObjectCodec;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class RestLoaderRequestDeserializer extends JsonDeserializer<RestLoaderRequest<IEntity>> {
|
||||
public class RestLoaderRequestDeserializer extends StdDeserializer<RestLoaderRequest<IEntity>> {
|
||||
private static final long serialVersionUID = -4245207329377196889L;
|
||||
|
||||
public RestLoaderRequestDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public RestLoaderRequestDeserializer(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestLoaderRequest<IEntity> deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
=========
|
||||
|
||||
## Fast-Json
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to JSON Schema in Java](http://www.baeldung.com/introduction-to-json-schema-in-java)
|
||||
- [A Guide to FastJson](http://www.baeldung.com/????????)
|
|
@ -13,6 +13,12 @@
|
|||
<version>1.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.13</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
|
|
@ -1,31 +1,32 @@
|
|||
package org.baeldung.json.schema;
|
||||
|
||||
import org.everit.json.schema.Schema;
|
||||
import org.everit.json.schema.loader.SchemaLoader;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JSONSchemaTest {
|
||||
|
||||
@Test
|
||||
public void givenInvalidInput_whenValidating_thenInvalid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_whenValidating_thenValid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
}
|
||||
package com.baeldung.json.schema;
|
||||
|
||||
import org.everit.json.schema.Schema;
|
||||
import org.everit.json.schema.ValidationException;
|
||||
import org.everit.json.schema.loader.SchemaLoader;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JSONSchemaTest {
|
||||
|
||||
@Test(expected = ValidationException.class)
|
||||
public void givenInvalidInput_whenValidating_thenInvalid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_whenValidating_thenValid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package fast_json;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.serializer.BeanContext;
|
||||
import com.alibaba.fastjson.serializer.ContextValueFilter;
|
||||
import com.alibaba.fastjson.serializer.NameFilter;
|
||||
import com.alibaba.fastjson.serializer.SerializeConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class FastJsonTests {
|
||||
private List<Person> listOfPersons;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
listOfPersons = new ArrayList<Person>();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2016, 6, 24);
|
||||
listOfPersons.add(new Person(15, "John", "Doe", calendar.getTime()));
|
||||
listOfPersons.add(new Person(20, "Janette", "Doe", calendar.getTime()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJavaList_thanConvertToJsonCorrect() {
|
||||
String personJsonFormat = JSON.toJSONString(listOfPersons);
|
||||
assertEquals(
|
||||
personJsonFormat,
|
||||
"[{\"FIRST NAME\":\"Doe\",\"LAST NAME\":\"John\",\"DATE OF BIRTH\":"
|
||||
+ "\"24/07/2016\"},{\"FIRST NAME\":\"Doe\",\"LAST NAME\":\"Janette\",\"DATE OF BIRTH\":"
|
||||
+ "\"24/07/2016\"}]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJson_thanConvertToObjectCorrect() {
|
||||
String personJsonFormat = JSON.toJSONString(listOfPersons.get(0));
|
||||
Person newPerson = JSON.parseObject(personJsonFormat, Person.class);
|
||||
assertEquals(newPerson.getAge(), 0); // serialize is set to false for age attribute
|
||||
assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName());
|
||||
assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateJson_thanGenerationCorrect() throws ParseException {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("FIRST NAME", "John" + i);
|
||||
jsonObject.put("LAST NAME", "Doe" + i);
|
||||
jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12");
|
||||
jsonArray.add(jsonObject);
|
||||
}
|
||||
assertEquals(
|
||||
jsonArray.toString(),
|
||||
"[{\"LAST NAME\":\"Doe0\",\"DATE OF BIRTH\":"
|
||||
+ "\"2016/12/12 12:12:12\",\"FIRST NAME\":\"John0\"},{\"LAST NAME\":\"Doe1\","
|
||||
+ "\"DATE OF BIRTH\":\"2016/12/12 12:12:12\",\"FIRST NAME\":\"John1\"}]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenContextFilter_whenJavaObject_thanJsonCorrect() {
|
||||
ContextValueFilter valueFilter = new ContextValueFilter() {
|
||||
public Object process(BeanContext context, Object object,
|
||||
String name, Object value) {
|
||||
if (name.equals("DATE OF BIRTH")) {
|
||||
return "NOT TO DISCLOSE";
|
||||
}
|
||||
if (value.equals("John") || value.equals("Doe")) {
|
||||
return ((String) value).toUpperCase();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
JSON.toJSONString(listOfPersons, valueFilter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSerializeConfig_whenJavaObject_thanJsonCorrect() {
|
||||
NameFilter formatName = new NameFilter() {
|
||||
public String process(Object object, String name, Object value) {
|
||||
return name.toLowerCase().replace(" ", "_");
|
||||
}
|
||||
};
|
||||
SerializeConfig.getGlobalInstance().addFilter(Person.class, formatName);
|
||||
String jsonOutput = JSON.toJSONStringWithDateFormat(listOfPersons,
|
||||
"yyyy-MM-dd");
|
||||
assertEquals(
|
||||
jsonOutput,
|
||||
"[{\"first_name\":\"Doe\",\"last_name\":\"John\","
|
||||
+ "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":"
|
||||
+ "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]");
|
||||
// resetting custom serializer
|
||||
SerializeConfig.getGlobalInstance().put(Person.class, null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package fast_json;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Person {
|
||||
|
||||
@JSONField(name = "AGE", serialize = false, deserialize = false)
|
||||
private int age;
|
||||
|
||||
@JSONField(name = "LAST NAME", ordinal = 2)
|
||||
private String lastName;
|
||||
|
||||
@JSONField(name = "FIRST NAME", ordinal = 1)
|
||||
private String firstName;
|
||||
|
||||
@JSONField(name = "DATE OF BIRTH", format = "dd/MM/yyyy", ordinal = 3)
|
||||
private Date dateOfBirth;
|
||||
|
||||
public Person() {
|
||||
|
||||
}
|
||||
|
||||
public Person(int age, String lastName, String firstName, Date dateOfBirth) {
|
||||
super();
|
||||
this.age = age;
|
||||
this.lastName = lastName;
|
||||
this.firstName = firstName;
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [age=" + age + ", lastName=" + lastName + ", firstName="
|
||||
+ firstName + ", dateOfBirth=" + dateOfBirth + "]";
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public Date getDateOfBirth() {
|
||||
return dateOfBirth;
|
||||
}
|
||||
|
||||
public void setDateOfBirth(Date dateOfBirth) {
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
}
|
||||
}
|
2
pom.xml
2
pom.xml
|
@ -45,7 +45,7 @@
|
|||
<!-- <module>jpa-storedprocedure</module> -->
|
||||
<module>querydsl</module>
|
||||
<!-- <module>raml</module> -->
|
||||
<module>rest-assured-tutorial</module>
|
||||
<module>rest-assured</module>
|
||||
<module>rest-testing</module>
|
||||
<module>resteasy</module>
|
||||
<module>log4j</module>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
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.baeldung</groupId>
|
||||
<artifactId>rest-assured-tutorial</artifactId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>rest-assured</name>
|
||||
<build>
|
|
@ -3,4 +3,5 @@
|
|||
<installed facet="wst.jsdt.web" version="1.0"/>
|
||||
<installed facet="jst.web" version="3.0"/>
|
||||
<installed facet="java" version="1.8"/>
|
||||
<installed facet="jpt.jpa" version="2.1"/>
|
||||
</faceted-project>
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.mvc.velocity.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ITutorialsService {
|
||||
|
||||
public List<Tutorial> listTutorials();
|
||||
List<Tutorial> listTutorials();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue