[英]Post XML to jersey rest webservice
i'm trying to post a message from a facelet (.xhtml) page to a REST web service (Jersey). 我试图将消息从facelet(.xhtml)页面发布到REST Web服务(泽西岛)。 I think it would be possible to do within javascript/jQuery if the server would have, let's say 我认为如果服务器拥有的话,可以在javascript / jQuery中进行操作,例如
<?php header('Access-Control-Allow-Origin: *'); ?>
However the server seems to not have a property like that, and I don't know where to modify that in the config. 但是服务器似乎没有这样的属性,而且我不知道在配置中在哪里修改它。
I tried this; 我试过了
var settings = {
"async": true,
"crossDomain": true,
"url": "example.com",
"method": "POST",
"headers": {
"content-type": "application/xml",
"cache-control": "no-cache"
},
"data":
"<consultation>\n \n\
<consultationDescription>"+description+"</consultationDescription>\n \n\
<customerName>"+fullName+"</customerName>\n \n\
<customerPhone>"+phonenumber+"</customerPhone>\n \n\
<endDateAndTime>"+endDateAndTime+"</endDateAndTime>\n \n\
<startDateAndTime>"+startDateAndTime+"</startDateAndTime>\n\n\
</consultation>",
contentType: "application/xml",
$.ajax(settings).done(function (response) {
console.log(response);
});
But I only get cross origin error. 但是我只会得到跨原点错误。
So should I try to find where to modify the origin on the server, or should I go with a different approach? 因此,我应该尝试找到在服务器上修改源的位置,还是应该采用其他方法? Maybe a < h:form > and post via a javabean like #{sendXML.someMethod} etc, but I don't know where to find the syntax for that. 也许<h:form>并通过像#{sendXML.someMethod}之类的javabean发布,但是我不知道在哪里可以找到它的语法。 I've been stuck with this problem for some time and can't find a good answer. 我已经被这个问题困扰了一段时间,找不到一个好的答案。 Do you guys have any idee what to do? 你们有什么想法吗?
Example: http://postimg.org/image/5k2thyl3p/ 示例: http : //postimg.org/image/5k2thyl3p/
Click green cell --> Write message (submit) --> book time on server. 单击绿色单元格->写消息(提交)->在服务器上预订时间。 Update view. 更新视图。
So I figured out how to do it by following this guide: http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ 因此,我按照以下指南找出了解决方法: http : //www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
private final String USER_AGENT = "Mozilla/5.0";
// HTTP POST request
private void sendPost() throws Exception {
try {
String url = "http://myurl";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "UTF-8");
con.setRequestProperty("content-type", "application/xml");
String urlParameters = "<myXML></myXML>";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
catch (Exception e) {
e.printStackTrace();
}
}
Since javascript did not work due to Cross Domain error, doing the work in a Javabean worked just fine. 由于javascript由于跨域错误而无法正常工作,因此在Javabean中进行工作就可以了。
I changed the code example by modifying Http s URLConnection to a HttpURLConnection and added con.setRequestProperty("content-type", "application/xml"). 我通过将Http 的 URLConnection修改为HttpURLConnection来更改了代码示例,并添加了con.setRequestProperty(“ content-type”,“ application / xml”)。
I also wrapped the statment with a try/catch. 我还用try / catch封装了该语句。
I probably stated my question poorly from the beginning. 我可能从一开始就不好说我的问题。 But by doing the operations in the bean it worked for me. 但是通过在bean中进行操作,它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.