更新的问题
我已经将我的客户端代码和restful服务代码修改为
更新的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文件吗
////////////////////////////////////////////////////////////////////////////////
////这是之前的问题,我从加瓦尔的帖子中得到了答案///我正在更新这个问题,现在我可以将xml文件发送到restful服务,如何在restful服务上读取这个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服务完全陌生,我必须从java客户端向Restful web服务发送系统位置(如c:/Files/Samle.xml
)中的XML文件,以前我曾向Restful发送json对象,但无法发送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)
{
}
就像我想把xml文件发送到restful服务一样,有人能有什么想法吗。。
替换字符串json="{\"EMPCODE\":\"125\",\"NAME_TBH\":"aaaa\"}";带有:
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();
更改:
@consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
也在您的客户端设置中:
httpCon.setRequestProperty("content-type","application/xml")
然后发送您的XML
数据。