简体   繁体   English

RESTful Web服务为POST请求返回不支持的媒体类型

[英]RESTful webservice returning Unsupported Media Type for POST requests

I have been working on a RESTful webservice using Jersey. 我一直在使用Jersey进行RESTful Web服务。 I have created my first service and now I am attempting to send requests. 我已经创建了第一个服务,现在正在尝试发送请求。 Here is my single POST method: 这是我的一个POST方法:

@POST
@Path("getQuote")
@Consumes(MediaType.WILDCARD )
@Produces(MediaType.APPLICATION_XML)
public Response getQuote(
        @FormParam("effectiveTime") String effectiveTime,
        @FormParam("responseType") String responseType,
        @FormParam("transform") String transform,
        @FormParam("data") String data,
        @FormParam("dataType") String dataType,
        @FormParam("source") String source) {

    String output = "";
    try {
        Map<String, String> formParams = new HashMap<String, String>();
        formParams.put("effectiveTime",effectiveTime);
        formParams.put("responseType",responseType);
        formParams.put("transform",transform);
        formParams.put("data",data);
        formParams.put("dataType",dataType);
        formParams.put("source",source);
        //do stuff
    } catch (Exception e) {
        System.out.println(e);
        output = "Error";
    }
    return Response.status(200).entity(output).build();
}

I have a HTML test form I can use to send data, and have checked the content type is correct using a RequestBin. 我有一个HTML测试表单,可以用来发送数据,并且已经使用RequestBin检查了内容类型是否正确。 Here are some of the headers from the request: 以下是请求中的一些标头:

Host: requestb.in
Content-Length: 16176
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0
Content-Type: application/x-www-form-urlencoded

I have tried the following content types with no luck: 我尝试了以下内容类型,但没有运气:

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Consumes("application/x-www-form-urlencoded")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Consumes(MediaType.APPLICATION_XML)
@Consumes(MediaType.WILDCARD )
& unspecified (no @Consumes annotation) 

I am unsure how to debug this request. 我不确定如何调试此请求。 I have tried using curl to test as well with the following params: 我已经尝试使用curl来测试以下参数:

curl --data "effectiveTime=1o1o1&dataType=lolo&data=lololol&transform=&responseType=lolol" http://localhost:8080/services/ahrdi/getQuote

with the same result. 结果相同。

也许尝试使用curl设置媒体类型标题

curl -i -H "Content-Type:application/x-www-form-urlencoded" --data "effectiveTime=1o1o1&dataType=lolo&data=lololol&transform=&responseType=lolol" http://localhost:8080/services/ahrdi/getQuote

eclipse was re using the original war without any alterations as I had not refreshed my project after running mvn compile war:war eclipse重新使用了原来的war,没有做任何改动,因为在运行mvn compile war:war之后我没有刷新我的项目

I ran mvn clean, refreshed my project in the project browser, ran mvn compile war:war and my restarted the server and requests are now accepted. 我运行了mvn clean,在项目浏览器中刷新了我的项目,运行了mvn compile war:war并重新启动了服务器,现在接受了请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM