axis2开发的webservice可以直接返回字符串吗

青蛙小王子 2013-04-15 09:47:40
最近使用axis2发布webservice,以前也没注意到这个问题,就是有的时候想直接返回一个字符串,但是在浏览器敲入地址显示出来的却是xml如下所示。
<ns:getVersionResponse><ns:return>Hi - the Axis2 version is 1.6.2</ns:return></ns:getVersionResponse>


自己就想直接返回一个字符串,也省了解析xml的工作,下面是我用HTTPConnection调用webservice的代码。
package com.tsp.webservice;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.imageio.ImageIO;

import org.dom4j.*;

public class TestService {
public static void main(String args[]) throws Exception {
Map<String, String> m = new HashMap<String, String>();
try {
System.out.println(loginCheck(m));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static String loginCheck(Map<String, String> parameter)
throws Exception {
// 拼接参数,这里最好使用StringBuilder

// StringBuilder builder = new StringBuilder();
// for (Map.Entry<String, String> entry : parameter.entrySet()) {
// // 这里的参数需要编码
// builder.append(entry.getKey() + "=").append(
// URLEncoder.encode(entry.getValue(), "UTF-8") + "&");
//
// }
// // 除去多余的&
// builder.deleteCharAt(builder.length() - 1);
// System.out.println(builder.toString());
// byte[] b2 = builder.toString().getBytes();
// 封装数据,数据以byte方式传输
// byte[] b = builder.toString().getBytes();
// 需要请求的连接
URL url = new URL(
"http://localhost:8080/axis2/services/Version/getVersion");
// 打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方式和消息头以及超时时间
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
// 设置是否允许对外输出数据
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(0));
// conn.setRequestProperty("Content-Length", "0");

// OutputStream outPut = conn.getOutputStream();
//
// outPut.write(b);
//
// outPut.close();

// 判断请求是否成功
if (conn.getResponseCode() == 200) {
InputStream in=conn.getInputStream();
byte[] b2=new byte[1024];
int len=0;
int temp=0;
while((temp=in.read())!=-1){
b2[len]=(byte)temp;
len++;
}
String str=new String(b2,0,len);
return str;
} else {
return "Err";
}

}
}


希望给我能给我一个解决办法,先在这里谢谢大家。
...全文
224 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
青蛙小王子 2013-12-26
  • 打赏
  • 举报
回复
这个是axis2规定好的格式,没办法改的。
keith0812 2013-12-25
  • 打赏
  • 举报
回复
你最后只如何解决这个问题的。能告诉一下吗,最近我也在找,但没有找到解决办法
s478853630 2013-04-15
  • 打赏
  • 举报
回复
貌似你这样只是发送了一次http请求,并没有调用axis2的服务, axis2功能很强大,不仅可以返回基本数据类型(比如String,int),还可以返回List,Map,自定义实体类等数据类型
lk198186 2013-04-15
  • 打赏
  • 举报
回复
ws服务返回的就是xml,想要字符串可以自己解析xml不就行了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧