2011.08.22 19:56
개발관련
1) 현재 환경이 JDK1.5 이므로, jersey 버전이 낮다(1.2 버젼이다. 1.3은 JDK1.6부터 지원한다). 해당 버전에서는 잘 알려진 버그가 있는데, json으로 array를 전송시, 한개 이상만, array로 보내고, 미만은 일반 객체로 넘겨 버린다. 클라이언트에서 관련 처리를 해주면 좋겠지만, 일관성 측면에서 서버측에서 지속적으로 array로 보낼 필요가 있을 때 이와 같은 방법을 사용해 볼수 있다.
2) 간단히 확장 클래스를 만든다. 아래는 인터넷에 뒤져보면 흔히 볼수 있는 코드와 비슷하다.
3) DI 한다. 이때 변환될 모델을 필요한 만큼 등록한다.
4) Test 코드
5) 결과
@Autowired로 DI 할수 있겠으나, 추가할 모델(Bean)들을 언제 늘어날지 모르므로 XML 설정파일로 하는게 편리할듯 싶다. 다른 버전에서는 잘 되는지 확인하지 않았다.
2) 간단히 확장 클래스를 만든다. 아래는 인터넷에 뒤져보면 흔히 볼수 있는 코드와 비슷하다.
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private final JAXBContext context;
private final Set<Class> types;
private Class<?>[] cTypes ;
public JAXBContextResolver(Class<?>[] __cTypes) throws Exception {
this.cTypes = __cTypes;
this.types = new HashSet(Arrays.asList(cTypes));
this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
}
public JAXBContext getContext(Class<?> objectType) {
return (types.contains(objectType)) ? context : null;
}
}
3) DI 한다. 이때 변환될 모델을 필요한 만큼 등록한다.
<bean class="com.xxx.jersey.resolver.JAXBContextResolver" scope="singleton">
<constructor-arg>
<list>
<value>xxx.domain.ChannelDsResponse</value>
<value>zzz.domain.CategoryDsResponse</value>
<value>yyy.domain.CountryDsResponse</value>
<value>www.DirtyWordsDsResponse</value>
</list>
</constructor-arg>
</bean>
4) Test 코드
@Test
public void testResponseJson() throws IllegalArgumentException, IOException {
ClientResponse clientResponse = webResource.path("/webservice/rest/Channel/KR/Channels/Category/01000000/1")
.accept("application/json").get(ClientResponse.class);
System.out.println("just 1: ===============================================");
System.out.println(clientResponse.getHeaders());
System.out.println(clientResponse.getEntity(String.class));
System.out.println("just 1: ===============================================");
clientResponse = webResource.path("/webservice/rest/Channel/KR/Channels/Category/01000000/2")
.accept("application/json").get(ClientResponse.class);
System.out.println("over 2: ===============================================");
System.out.println(clientResponse.getHeaders());
System.out.println(clientResponse.getEntity(String.class));
System.out.println("over 2: ===============================================");
}
5) 결과
just 1: =============================================== {Date=[Mon, 22 Aug 2011 10:46:50 GMT], Server=[Apache-Coyote/1.1], Content-Type=[ {"resultCode":0,"resultMessage":"","channels":[{"categories":[{"id":"01000000"}], just 1: =============================================== over 2: =============================================== {Date=[Mon, 22 Aug 2011 10:46:50 GMT], Server=[Apache-Coyote/1.1], Content-Type=[ {"resultCode":0,"resultMessage":"","channels":[{"categories":[{"id":"01000000"}], over 2: ===============================================
@Autowired로 DI 할수 있겠으나, 추가할 모델(Bean)들을 언제 늘어날지 모르므로 XML 설정파일로 하는게 편리할듯 싶다. 다른 버전에서는 잘 되는지 확인하지 않았다.
'개발관련' 카테고리의 다른 글
Flex(ActionScript) ExternalInterface.call 혹은 addCallback 예제 (0) | 2011.09.01 |
---|---|
spirng-roo-addon-max OSGi bundle 업데이트 방법 (0) | 2011.08.26 |
Jersey에서 JSON Array 버그(jersey-json)에 대한 ContextResolver 확장 (0) | 2011.08.22 |
Spring Datastore Document MongoDB 사용 예시 (0) | 2011.08.13 |
akka non blocking Test (0) | 2011.08.11 |
spring-roo-addon-max osgi + show screencast (0) | 2011.07.08 |