general java
JAXB / Http Client unmarshaller example(샘플)
'김용환'
2012. 2. 14. 19:18
apache commons의 http client와 jaxb로 어떻게 테스트할지에 대해서 작성한 글이다.
특정 URL을 호출하면 사원 정보가 나온다고 가정한다.
<ROOT>
<EMPLOYEE>
<id></id>
<nm></nm>
….
</EMPLOYEE>
<EMPLOYEE>
<id></id>
<nm></nm>
….
</EMPLOYEE>
</ROOT> |
객체는 Root-List<Employee>의 구조로 생각,
# Root.java 소스
package test;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "ROOT")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElement(name="EMPLOYEE")
private List<Employee> employees;
public Root() {
employees = new ArrayList<Employee>();
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
|
# Employee.java
package test;
public class Employee {
private String id;
private String nm;
….
… // set, get
} |
찾고 싶은 사람의 사번을 작성한다.
# XEmployee.java
public class XEmployee {
private static List<String> employeeList;
public static List<String> getEmployeeList() {
return employeeList;
}
static {
employeeList = new ArrayList<String>();
employeeList.add("aaa");
employeeList.add("bbb");
employeeList.add("ccc");
…
}
} |
찾고 싶은 사람을 찾아서 출력한다.
# main 메서드
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class CheckEmp {
public static final boolean isSaved = false;
public static final String URL = "http://xxxxxxxx";
public static JAXBContext jaxbContext;
static {
try {
jaxbContext = JAXBContext
.newInstance(new Class[] { test.Root.class });
} catch (JAXBException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// connect url
BufferedReader reader = connectURL();
// save file
if (isSaved) {
saveFile(reader);
}
// get object
Root root = getRoot(reader);
// find
for (String retiredEmpID : XEmployee.getEmployeeList()) {
boolean found = false;
for(Employee emp : root.getEmployees()) {
if (emp.getID().trim().equalsIgnoreCase(retiredEmpID)) {
found = true;
}
}
if (found == false) {
System.out.println(retiredEmpID);
}
}
}
private static Root getRoot(BufferedReader reader) throws JAXBException {
Root root = (Root) jaxbContext.createUnmarshaller().unmarshal(reader);
return root;
}
private static BufferedReader connectURL() throws IOException,
ClientProtocolException, UnsupportedEncodingException {
HttpGet httpget = new HttpGet(URL);
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter("http.socket.timeout", 999000);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity == null) {
throw new RuntimeException("url error");
}
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "utf-8"));
return reader;
}
private static void saveFile(BufferedReader reader) throws IOException {
Writer fstream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("aaa.txt"), "utf-8"));
BufferedWriter out = new BufferedWriter(fstream);
char[] a = new char[1024] ;
while (reader.read(a) > 0) {
out.write(a);
}
out.flush();
System.out.println("File created successfully.");
}
}
|
# pom.xml
<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>
<groupId>test1123</groupId>
<artifactId>teswt1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>