INTSAMPLES-127 Polishing
- PR Comments - Switch to Jackson2 - Fix oxm config Polishing INTSAMPLES-127 Update to SI 4.0.0.RC1 INTSAMPLES-127 Remove Servlet Dependency travel sample; see INT-3350. INTSAMPLES-127 Update Samples to SI 4.0.0 JIRA: https://jira.spring.io/browse/INTSAMPLES-127 Created `maint` branch for ongoing SI 3.0.x samples.
This commit is contained in:
committed by
Artem Bilan
parent
c5b1ace70f
commit
cc579226d6
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
package org.springframework.integration.samples.rest.json;
|
||||
|
||||
import org.codehaus.jackson.map.AnnotationIntrospector;
|
||||
import org.codehaus.jackson.map.DeserializationConfig;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;
|
||||
import com.fasterxml.jackson.databind.AnnotationIntrospector;
|
||||
import com.fasterxml.jackson.databind.DeserializationConfig;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
||||
|
||||
|
||||
/**
|
||||
* JaxbJacksonObjectMapper.java: This is the custom JAXB JSON ObjectMapper
|
||||
@@ -27,22 +28,26 @@ import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;
|
||||
* http://hillert.blogspot.com/2011/01/marshal-json-data-using-jackson-in.html.
|
||||
* I modified a little bit to use the latest {@link DeserializationConfig} API
|
||||
* instead of deprecated ones.
|
||||
* <p> *
|
||||
* <p>
|
||||
* Updated to Jackson2.
|
||||
* <p>
|
||||
* @author Vigil Bose
|
||||
* @author Gary Russell
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class JaxbJacksonObjectMapper extends ObjectMapper {
|
||||
|
||||
/**
|
||||
* Annotation introspector to use for serialization process
|
||||
* Annotation introspector to use for serialization process
|
||||
* is configured separately for serialization and deserialization purposes
|
||||
*/
|
||||
public JaxbJacksonObjectMapper() {
|
||||
final AnnotationIntrospector introspector
|
||||
= new JacksonAnnotationIntrospector();
|
||||
super.getDeserializationConfig()
|
||||
.withAnnotationIntrospector(introspector);
|
||||
.with(introspector);
|
||||
super.getSerializationConfig()
|
||||
.withAnnotationIntrospector(introspector);
|
||||
|
||||
.with(introspector);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,17 +17,18 @@ package org.springframework.integration.samples.rest.json.view;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
|
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
|
||||
|
||||
/**
|
||||
* ExtendedMappingJacksonJsonView.java: This class extends the Spring's MappingJacksonJsonView
|
||||
* <p>
|
||||
* Note: The source code for this class is taken from the forum posted by AhungerArtist
|
||||
* at http://forum.springsource.org/archive/index.php/t-84006.html
|
||||
* </p>
|
||||
* <p>
|
||||
* @author Vigil Bose
|
||||
* @author Gary Russell
|
||||
*/
|
||||
public class ExtendedMappingJacksonJsonView extends MappingJacksonJsonView {
|
||||
public class ExtendedMappingJacksonJsonView extends MappingJackson2JsonView {
|
||||
|
||||
@SuppressWarnings({"rawtypes" })
|
||||
@Override
|
||||
@@ -36,10 +37,10 @@ public class ExtendedMappingJacksonJsonView extends MappingJacksonJsonView {
|
||||
if (!(result instanceof Map)){
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Map map = (Map) result;
|
||||
if (map.size() == 1){
|
||||
return map.values().toArray()[0];
|
||||
return map.values().toArray()[0];
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHeaders;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.integration.samples.rest.domain.Employee;
|
||||
import org.springframework.integration.samples.rest.domain.EmployeeList;
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
|
||||
@@ -61,7 +61,7 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.integration.samples.rest.domain" />
|
||||
<oxm:jaxb2-marshaller id="marshaller" context-path="org.springframework.integration.samples.rest.domain" />
|
||||
|
||||
<int:service-activator id="employeeServiceActivator"
|
||||
input-channel="employeeSearchRequest"
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.integration.samples.rest;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
@@ -25,10 +26,10 @@ import java.util.Map;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -45,9 +46,11 @@ import org.springframework.web.client.HttpMessageConverterExtractor;
|
||||
import org.springframework.web.client.RequestCallback;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* RestHttpClientTest.java: Functional Test to test the REST HTTP Path usage. This test requires
|
||||
* rest-http application running in HTTP environment.
|
||||
* rest-http application running in HTTP environment.
|
||||
* @author Vigil Bose
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@@ -66,14 +69,14 @@ public class RestHttpClientTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
responseExtractor = new HttpMessageConverterExtractor<EmployeeList>(EmployeeList.class, restTemplate.getMessageConverters());
|
||||
|
||||
|
||||
Map<String, Object> properties = new HashMap<String, Object>();
|
||||
properties.put(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8");
|
||||
properties.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
marshaller.setMarshallerProperties(properties);
|
||||
}
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
@@ -107,21 +110,23 @@ public class RestHttpClientTest {
|
||||
employeeSearchMap.put("id", id);
|
||||
return employeeSearchMap;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetEmployeeAsJson() throws Exception{
|
||||
Map<String, Object> employeeSearchMap = getEmployeeSearchMap("0");
|
||||
|
||||
|
||||
final String fullUrl = "http://localhost:8080/rest-http/services/employee/{id}/search?format=json";
|
||||
HttpHeaders headers = getHttpHeadersWithUserCredentials(new HttpHeaders());
|
||||
headers.add("Accept", "application/json");
|
||||
HttpEntity<Object> request = new HttpEntity<Object>(headers);
|
||||
|
||||
|
||||
ResponseEntity<?> httpResponse = restTemplate.exchange(fullUrl, HttpMethod.GET, request, EmployeeList.class, employeeSearchMap);
|
||||
logger.info("Return Status :"+httpResponse.getHeaders().get("X-Return-Status"));
|
||||
logger.info("Return Status Message :"+httpResponse.getHeaders().get("X-Return-Status-Msg"));
|
||||
assertTrue(httpResponse.getStatusCode().equals(HttpStatus.OK));
|
||||
jaxbJacksonObjectMapper.writeValue(System.out, httpResponse.getBody());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
jaxbJacksonObjectMapper.writeValue(out, httpResponse.getBody());
|
||||
logger.info(new String(out.toByteArray()));
|
||||
}
|
||||
|
||||
private HttpHeaders getHttpHeadersWithUserCredentials(ClientHttpRequest request){
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<property name="marshaller" ref="marshaller"/>
|
||||
<property name="unmarshaller" ref="marshaller"/>
|
||||
</bean>
|
||||
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
|
||||
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
|
||||
<property name="objectMapper" ref="jaxbJacksonObjectMapper"/>
|
||||
</bean>
|
||||
</list>
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
<context:component-scan base-package="org.springframework.integration.samples.rest"/>
|
||||
|
||||
<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.integration.samples.rest.domain" />
|
||||
<oxm:jaxb2-marshaller id="marshaller" context-path="org.springframework.integration.samples.rest.domain" />
|
||||
|
||||
<bean id="jaxbJacksonObjectMapper" class="org.springframework.integration.samples.rest.json.JaxbJacksonObjectMapper"/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user