执行从Ext.Ajax.request一个PUT时的球衣web服务返回一个415球衣、request、Ext、Ajax

2023-09-10 21:39:53 作者:风吻过你的脸

所以,我已经有了一个pretty的简单的RESTful Web服务的球衣,而我想从我的javascript code调用它。我没有问题,执行GET请求,但由于某些原因,当我执行一个PUT,我回来了415不支持的媒体类型错误。知不知道我做错了什么?

So, I've got a pretty simple RESTful jersey webservice, and I'm wanting to call it from my javascript code. I have no problem executing a GET request, but for some reason, when I execute a PUT, I get back a "415 Unsupported Media Type" error. Any idea what I'm doing wrong?

下面是我的web服务:

Here's my webservice :

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:

Here's my annotated 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 code:

And finally, here's my javascript code :

<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/>

感谢您抽空看看!

Thanks for taking a look!

推荐答案

由于Web服务code期待JSON --- @消耗({MediaType.APPLICATION_JSON})---- 这就是为什么它失败了,当你的请求通过不同的媒体类型格式。你的固定工作,因为你没有设置你的要求的内容类型的JSON数据。

Since your web service code expecting JSON ---@Consumes({MediaType.APPLICATION_JSON})---- that's why it failed when your request comes through with different media type format. Your fixed works because you did set your request content type to JSON data.