更新了问题
我已将我的客户端代码和宁静的服务代码修改为
更新的cliet代码是
URL url = new URL("http://localhost:9090/XsdValidation/api/data/xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");
////
StringBuilder builder = new StringBuilder();
FileReader fileReader = new FileReader("Employee.xml");
BufferedReader reader = new BufferedReader(fileReader);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
builder .append(line);
}
String xml = builder .toString();
System.out.println("xml file is "+xml);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(xml);
out.close();
reader.close();
更新了rest ful服务代码
@Path("/data")
public class DataAccess {
@POST
@Path("/xml")
@Consumes(MediaType.APPLICATION_XML)
public String readXml(String file) {
System.out.println("in xml");
return file;
}
}
一切都很好我得到的字符串是<?xml version="1.0" encoding="UTF-8"?><empns:employee xmlns:empns="Symplocus/Employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Symplocus/Employee Employee.xsd "> <empns:EMP_ID>0101</empns:EMP_ID> <empns:NAME>Rajasekhar</empns:NAME> <empns:SALARY>2000</empns:SALARY> <empns:DATEOFJOINING>2001-01-01</empns:DATEOFJOINING></empns:employee>
我想将整个字符串转换为xml文件,以便使用xsd进行验证..任何人都有想法将字符串转换为XML文件
/////////////////////////////////////////////// /////////////////////////////////
////这是先前我从GAgarwarl的帖子中得到答案的问题/// 我正在更新问题,现在我能够发送xml文件来恢复服务,如何阅读这个读取这个xml上的宁静服务
我的客户端代码是
URL url = new URL("http://localhost:9090/XsdValidation/api/data/xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");
OutputStream os = connection.getOutputStream();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
FileReader fileReader = new FileReader("Employee.xml");
StreamSource source = new StreamSource(fileReader);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
os.flush();
System.out.println(connection.getResponseCode());
//connection.disconnect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
System.out.println("in input stream");
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
System.out.println(connection.getResponseCode());
System.out.println("end of client programme");
我的宁静代码是
@Path("/data")
public class DataAccess {
@POST
@Path("/xml")
@Consumes(MediaType.APPLICATION_XML)
public String readXml(/here i have to read file /) {
// here i want an idea to read the file sent by client
}
}
////这是个老问题////
我是Web服务的新手,我必须在系统位置发送一个XML文件,如c:/Files/Samle.xml
,从java客户端发送到Restful web服务,以前我已经发送json对象到restful但是无法发送xml
发送json的客户端代码是
String tableName="SYMPLOCUS.IMDB1_FINANCE_BUDGE ";
String urlString="http://localhost:9090/DataAccess/api/DataAccess/loadData?tableName="+tableName;
URL url=new URL(urlString);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type",
"application/json");
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
String json = "{\"EMPCODE\":\"125\", \"NAME_TBH\":\"aaaaa\"}";
out.write(json);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
System.out.println("in input stream");
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
System.out.println(httpCon.getResponseCode());
我的宁静代码是
@Path("/loadData")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String sendReceiveJson(String data,@QueryParam("tableName") String tableName)
{
}
以同样的方式我将魔法文件发送到宁静的服务,可以有人有任何想法..
答案 0 :(得分:1)
变化:
@consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
并且在您的客户端设置:
httpCon.setRequestProperty("content-type","application/xml")
然后发送您的XML
数据。
答案 1 :(得分:1)
替换 String json =“{\”EMPCODE \“:\”125 \“,\”NAME_TBH \“:\”aaaaa \“}”; 用:
StringBuilder builder = new StringBuilder();
FileReader fileReader = new FileReader("fileName");
BufferedReader reader = new BufferedReader(fileReader);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
builder .append(line);
}
String xml = builder .toString();