Axis2 시리즈의 대상 유형에 대한 간단한 예【3】
5473 단어 axis2
1. 서버측 코드
일단 bean을 만들겠습니다.package data;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
참고: 이 bean에는 패키지가 있습니다.
다음 서비스 방법에는 패키지가 없습니다import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import data.User;
public class SimpleService {
public String upload4Byte(byte[] b, int len) {
String path = "";
FileOutputStream fos = null;
String dir = System.getProperty("user.dir");
File file = new File(dir + "/" + new Random().nextInt(100) + ".jsp");
try {
fos = new FileOutputStream(file);
fos.write(b, 0, len);
path = file.getAbsolutePath();
System.out.println("file path:" + file.getAbsolutePath());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return path;
}
public int[] getArray(int i) {
int[] array = new int[i];
for (int j = 0; j < i; j++) {
array[j] = new Random().nextInt(1000);
}
return array;
}
public String[][] getTwoArray() {
return new String[][] { { " ", " " }, { " ", " " }};
}
public User getUer() {
User user = new User();
user.setAddress("JingsanRoad");
user.setEmail("[email protected]");
user.setName("spark");
user.setId(2);
return user;
}
}
SimpleService를class 파일을 포조 폴더로 복사
주의해야 할 것은 이 User 대상의 패키지는 데이터입니다.tomcat 디렉터리에 있는 웹 앱의 axis2의 WEB-INF 디렉터리에 데이터 디렉터리를 만들어서 User 대상의 디렉터리와 일치해야 합니다.그렇지 않으면 WebService에서 ClassNotFontException 예외가 발생합니다.그리고 당신의tomcat을 다시 시작합니다. 비록 axis2는 열배치를 지원하지만
2. 클라이언트 코드 package com.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import data.User;
public class SimpleServiceClient {
public static void main(String args[]) {
RPCServiceClient client;
try {
client = new RPCServiceClient();
Options options = client.getOptions();
String address = "http://localhost:8080/axis2/services/SimpleService";
String defualNameSpace = "http://ws.apache.org/axis2";
EndpointReference epr = new EndpointReference(address);
options.setTo(epr);
QName qname = new QName(defualNameSpace, "upload4Byte");
String path = System.getProperty("user.dir");
File file = new File(path + "/WebRoot/index.jsp");
FileInputStream fis = new FileInputStream(file);
int len = (int) file.length();
byte[] b = new byte[len];
int read = fis.read(b);
fis.close();
Object[] result = client.invokeBlocking(qname, new Object[] { b,
len }, new Class[] { String.class });
System.out.println("upload4Byte:" + result[0]);
qname = new QName(defualNameSpace, "getTwoArray ");
result = client.invokeBlocking(qname, new Object[] {},
new Class[] { String[][].class });
String[][] strs = (String[][]) result[0];
System.out.println("getTwoArray :");
for (String[] s : strs) {
for (String str : s) {
System.out.println(str);
}
}
// for(int i=0;i<strs.length;i++){
// String[] temp=strs[i];
// for(int j=0;j<temp.length;j++){
// System.out.println(temp[j]);
// }
// }
qname = new QName(defualNameSpace, "getArray ");
result = client.invokeBlocking(qname, new Object[] { 3 },
new Class[] { int[].class });
int[] arr = (int[]) result[0];
System.out.println("getArray: ");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
qname = new QName(defualNameSpace, "getUer");
result = client.invokeBlocking(qname, new Object[] {},
new Class[] { User.class });
System.out.println("getUer:");
User user = (User) result[0];
System.out.println("name=" + user.getName());
System.out.println("address=" + user.getAddress());
} catch (AxisFault e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
결과 출력:
upload4Byte:C:\Program Files\apache-tomcat-6.0.24\bin\69.jspgetTwoArray: 중국 베이징 일본 도쿄 getarray: 23156411getUer:name=sparkaddress=JingsanRoad
package data;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import data.User;
public class SimpleService {
public String upload4Byte(byte[] b, int len) {
String path = "";
FileOutputStream fos = null;
String dir = System.getProperty("user.dir");
File file = new File(dir + "/" + new Random().nextInt(100) + ".jsp");
try {
fos = new FileOutputStream(file);
fos.write(b, 0, len);
path = file.getAbsolutePath();
System.out.println("file path:" + file.getAbsolutePath());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return path;
}
public int[] getArray(int i) {
int[] array = new int[i];
for (int j = 0; j < i; j++) {
array[j] = new Random().nextInt(1000);
}
return array;
}
public String[][] getTwoArray() {
return new String[][] { { " ", " " }, { " ", " " }};
}
public User getUer() {
User user = new User();
user.setAddress("JingsanRoad");
user.setEmail("[email protected]");
user.setName("spark");
user.setId(2);
return user;
}
}
package com.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import data.User;
public class SimpleServiceClient {
public static void main(String args[]) {
RPCServiceClient client;
try {
client = new RPCServiceClient();
Options options = client.getOptions();
String address = "http://localhost:8080/axis2/services/SimpleService";
String defualNameSpace = "http://ws.apache.org/axis2";
EndpointReference epr = new EndpointReference(address);
options.setTo(epr);
QName qname = new QName(defualNameSpace, "upload4Byte");
String path = System.getProperty("user.dir");
File file = new File(path + "/WebRoot/index.jsp");
FileInputStream fis = new FileInputStream(file);
int len = (int) file.length();
byte[] b = new byte[len];
int read = fis.read(b);
fis.close();
Object[] result = client.invokeBlocking(qname, new Object[] { b,
len }, new Class[] { String.class });
System.out.println("upload4Byte:" + result[0]);
qname = new QName(defualNameSpace, "getTwoArray ");
result = client.invokeBlocking(qname, new Object[] {},
new Class[] { String[][].class });
String[][] strs = (String[][]) result[0];
System.out.println("getTwoArray :");
for (String[] s : strs) {
for (String str : s) {
System.out.println(str);
}
}
// for(int i=0;i<strs.length;i++){
// String[] temp=strs[i];
// for(int j=0;j<temp.length;j++){
// System.out.println(temp[j]);
// }
// }
qname = new QName(defualNameSpace, "getArray ");
result = client.invokeBlocking(qname, new Object[] { 3 },
new Class[] { int[].class });
int[] arr = (int[]) result[0];
System.out.println("getArray: ");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
qname = new QName(defualNameSpace, "getUer");
result = client.invokeBlocking(qname, new Object[] {},
new Class[] { User.class });
System.out.println("getUer:");
User user = (User) result[0];
System.out.println("name=" + user.getName());
System.out.println("address=" + user.getAddress());
} catch (AxisFault e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
결과 출력:
upload4Byte:C:\Program Files\apache-tomcat-6.0.24\bin\69.jspgetTwoArray: 중국 베이징 일본 도쿄 getarray: 23156411getUer:name=sparkaddress=JingsanRoad