所以,我有一个非常简单的RESTful球衣网络服务,我想从我的javascript代码中调用它。我执行GET请求没有问题,但出于某种原因,当我执行PUT时,我收到“415 Unsupported Media Type”错误。知道我做错了吗?
这是我的网络服务:
package servlet;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBElement;
import com.mycompany.www.hudsonsemaphore.HudsonResult;
import com.mycompany.www.hudsonsemaphore.SemaphoreConstants;
@Path("/HudsonSemaphore")
public class HudsonSemaphore {
static private HudsonResult lastResult;
static private Object RESULT_LOCK = new Object();
@PUT
@Consumes({MediaType.APPLICATION_JSON})
static public void setResult(JAXBElement<HudsonResult> result) {
synchronized(RESULT_LOCK) {
lastResult=result.getValue();
}
}
@GET
@Produces({MediaType.APPLICATION_JSON})
public HudsonResult getLastResult() {
HudsonResult toReturn;
if(lastResult!=null) {
synchronized(RESULT_LOCK) {
toReturn = lastResult;
lastResult=null;
}
}
else {
toReturn = SemaphoreConstants.NULL_HUDSON_RESULT;
}
return toReturn;
}
}
这是我的带注释的JAXBElement:
package com.mycompany.www.hudsonsemaphore;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="hudsonResult")
@XmlType(propOrder = {
"message", "success"
})
public class HudsonResult {
private boolean success;
private String message;
public HudsonResult() {
success=false;
message=null;
}
public HudsonResult(String message, boolean success) {
this.success = success;
this.message = message;
}
@XmlElement
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message=message;
}
public void setSuccess(boolean success) {
this.success=success;
}
@XmlElement
public boolean isSuccess() {
return success;
}
public boolean equals(HudsonResult result) {
return(result.success == success && result.message.equals(message));
}
public String toString() {
return "Success : [" + String.valueOf(success) + "] Message : [" + message + "]";
}
}
最后,这是我的javascript代码:
<script type="text/javascript" src="/system/workplace/resources/ext/ext-base.js"></script>
<script type="text/javascript" src="/system/workplace/resources/ext/ext-all.js"></script>
<script type="text/javascript">
function pollSemaphore() {
Ext.Ajax.request({
url : 'http://localhost:8081/leadcapture/rest/HudsonSemaphore',
method: 'GET',
success:
function(result, request) {
var jsonData = Ext.util.JSON.decode(result.responseText);
if(jsonData.success=='true') {
alert('SUCCESS : ' + jsonData.message);
}
else {
alert('NOT SUCCESS : ' + result.responseText);
}
},
failure:
function(result, request) {
alert('Teh fail : ' + result.responseText);
}
});
}
function pushSemaphore() {
Ext.Ajax.request({
url : 'http://localhost:8081/leadcapture/rest/HudsonSemaphore',
method: 'PUT',
params: {message : 'Brand new car!', success : 'true'},
success:
function(result, request) {
alert('YOU RULE!');
},
failure:
function(result, request) {
alert('Teh fail : ' + result.responseText);
}
});
}
</script>
<input type="button" onclick="pushSemaphore()" value="Push"/> <br/>
<input type="button" onclick="pollSemaphore()" value="Poll"/> <br/>
谢谢你看看!
答案 0 :(得分:2)
由于您的Web服务代码需要JSON --- @ Consumes({MediaType.APPLICATION_JSON})---- 这就是为什么当您的请求通过不同的媒体类型格式时失败的原因。您的固定工作是因为您确实将请求内容类型设置为JSON数据。
答案 1 :(得分:1)
好的,我想我想出了这个。显然,您希望为Ext.Ajax.request方法提供 jsonData 参数,而不是 params 参数。这真是奇怪,因为所有的例子都告诉你使用params参数,这给了我415错误。无论如何,pushSemaphore()函数的工作版本如下所示:
function pushSemaphore() {
Ext.Ajax.request({
url : 'http://localhost:8081/leadcapture/rest/HudsonSemaphore',
method: 'PUT',
jsonData: {message : 'Brand new car!', success : 'true'},
success:
function(result, request) {
alert('YOU RULE!');
},
failure:
function(result, request) {
alert('Teh fail : ' + result.responseText);
}
});
}
所以,我有这个工作很好,但是对我来说这仍然是个谜,为什么它有效,以及为什么当我按照文档中的例子时它失败了。有关此事的任何想法?