这篇文章主要围绕JavaHTTP415JSON不支持的媒体类型错误和java错误不支持发行版本5展开,旨在为您提供一份详细的参考资料。我们将全面介绍JavaHTTP415JSON不支持的媒体类型错误的
这篇文章主要围绕Java HTTP 415 JSON不支持的媒体类型错误和java错误不支持发行版本5展开,旨在为您提供一份详细的参考资料。我们将全面介绍Java HTTP 415 JSON不支持的媒体类型错误的优缺点,解答java错误不支持发行版本5的相关问题,同时也会为您带来415 Spring MVC和Rest Service不支持的媒体类型、415不支持的媒体类型-lightswitch 2012中的POST json到OData服务、415不支持的媒体类型; Angular2到API、415不支持的媒体类型jQuery Ajax的实用方法。
本文目录一览:- Java HTTP 415 JSON不支持的媒体类型错误(java错误不支持发行版本5)
- 415 Spring MVC和Rest Service不支持的媒体类型
- 415不支持的媒体类型-lightswitch 2012中的POST json到OData服务
- 415不支持的媒体类型; Angular2到API
- 415不支持的媒体类型jQuery Ajax
Java HTTP 415 JSON不支持的媒体类型错误(java错误不支持发行版本5)
我正在用JSON请求调用REST服务,它给出了Http 415“不支持的媒体类型”错误。
请求内容类型设置为(“ Content-Type”,“ application / json; charset = utf8”)。
如果我在请求中不包含Json对象,则效果很好。我正在使用json的google-gson-2.2.4库。
我尝试使用几个不同的库,但这没什么区别。
有人可以帮我解决这个问题吗?
这是我的代码:
public static void main(String[] args) throws Exception{ JsonObject requestJson = new JsonObject(); String url = "xxx"; //method call for generating json requestJson = generateJSON(); URL myurl = new URL(url); HttpURLConnection con = (HttpURLConnection)myurl.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "application/json; charset=utf8"); con.setRequestProperty("Accept", "application/json"); con.setRequestProperty("Method", "POST"); OutputStream os = con.getOutputStream(); os.write(requestJson.toString().getBytes("UTF-8")); os.close(); StringBuilder sb = new StringBuilder(); int HttpResult =con.getResponseCode(); if(HttpResult ==HttpURLConnection.HTTP_OK){ BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); System.out.println(""+sb.toString()); }else{ System.out.println(con.getResponseCode()); System.out.println(con.getResponseMessage()); } }public static JsonObject generateJSON () throws MalformedURLException{ String s = "http://www.abc.com"; s.replaceAll("/", "\\/"); JsonObject reqparam=new JsonObject(); reqparam.addProperty("type", "arl"); reqparam.addProperty("action", "remove"); reqparam.addProperty("domain", "staging"); reqparam.addProperty("objects", s); return reqparam;}}
requestJson.toString的值为:
{“ type”:“ arl”,“ action”:“删除”,“ domain”:“ staging”,“ objects:” http://www.abc.com “}
答案1
小编典典不确定原因,但是charset=utf8
从中 删除行con.setRequestProperty("Content-Type", "application/json; charset=utf8")
解决了该问题。
415 Spring MVC和Rest Service不支持的媒体类型
我正进入(状态
415 Unsupported Media Type - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
请求网址为:
http://localhost:8080/ngdemo/web/posts/review/80a5d7660cdb82a8ef9f8db79bb3c8ab14555377
从弹簧控制器读取时出错;我检查了其他具有相同模式的控制器方法,它们工作正常,但不是我新介绍的这种方法。我找不到任何问题,能否请您提出我所缺少的内容?
我的控制器:
@RequestMapping(value = "/review/{key}", method = RequestMethod.GET, consumes = "", produces = "application/json")public@ResponseBodyList<Review> reviews(@PathVariable(value = "key") String key) { System.out.println("key : " + key); List<Review> reviewList = reviewService.getReviewsById(key); System.out.println("reviewList : " + reviewList.size()); return reviewList;}
我的Angular Services.js:
services.factory(''PostFactory'', [''$resource'', function ($resource) {alert("I am here service");return { postmain: $resource(''/ngdemo/web/posts'', {}, { query: {method: ''GET'', isArray: true }, create: {method: ''POST''} }), reviews: $resource(''/ngdemo/web/posts/review/:key'', {}, { query: {method: ''GET'', params: {key: ''@key''} }, create: {method: ''POST''} }), postreview: $resource(''/ngdemo/web/posts/getreview'', {}, { query: {method: ''GET'', isArray: true }, create: {method: ''POST''} }), allresults: $resource(''/ngdemo/web/posts/result/:tag'', {}, { query: {method: ''GET'', params: {tag: ''@tag''} }, create: {method: ''POST''} })};
}]);
我的controller.js中的代码可以调用:
var reviewId = place.id;$scope.allreviews = PostFactory.reviews.query({key: reviewId})
我找不到问题出在哪里,所以请大家看看并指出我想念的是什么?谢谢!
答案1
小编典典它通过添加以下内容起作用:
@Consumes("text/html") @Consumes("text/html")@RequestMapping(value = "/review/{key}", method = RequestMethod.GET, produces = "application/json")public@ResponseBodyList<Review> reviews(@PathVariable(value = "key") String key) {
415不支持的媒体类型-lightswitch 2012中的POST json到OData服务
使用JSON发布到OData服务时,出现“错误415:不支持的媒体类型”。
解决方案位于此较长文章的底部。
我可以使用JSON进行获取,但是一旦尝试POST,我就会收到此错误。
我也可以使用XML进行GET / POST,但是我需要使用json。
我认为此错误是指我的标头中的某些内容,而不是我的请求正文的json格式(在下面也可能是错误的),我尝试了多种变体导致相同的错误。
我一直在尝试使用Fiddler进行调试,以下是结果。
JSON开机自检
请求
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
User-Agent: Fiddler
Host: scdb38:8888
Content-Length: 91
Accept: application/json;odata=verbose;
Content-Type: application/json;
{
"d":[
{
"Name":"Great White ","Food":"Surfers"
}
]
}
响应
HTTP/1.1 415 Unsupported Media Type
Cache-Control: private
Content-Length: 186
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed,16 Oct 2013 06:20:10 GMT
{"error":{"code":"1","message":{"lang":"en-AU","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>Unsupported media type requested.</Message></ExceptionInfo>"}}}
JSON GET
请求标题
GET http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
User-Agent: Fiddler
Host: scdb38:8888
Content-Length: 0
Accept: application/json;odata=verbose;
Content-Type: application/json;
响应头
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Length: 591
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed,16 Oct 2013 06:23:41 GMT
{"d":[{"__metadata":{"id":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(1)","uri":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(1)","etag":"W/\"X'00000000000007E4'\"","type":"LightSwitchApplication.Shark"},"Id":1,"RowVersion":"AAAAAAAAB+Q=","Name":"Tiger Shark","Food":"Penguins"},{"__metadata":{"id":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(2)","uri":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(2)","etag":"W/\"X'00000000000007E5'\"","Id":2,"RowVersion":"AAAAAAAAB+U=","Name":"Grey Nurse","Food":"Lettuce"}]}
我不太明白为什么GET可以正常工作时POST不支持媒体类型的原因。我意识到我正在使用DataServiceVersion:1.0并已查找升级,但是我正在使用LightSwitch
2012并在引用新版本时遇到问题,而又没有破坏LightSwitch应用程序。我认为LightSwitch
2013使用的是较新的版本(?),但是对我而言,升级带来了新的(非技术性)挑战。我感觉自己正在绕过圈子,这是我的最后选择,与同事交谈后,我唯一的失败选择是在数据库上创建数据模型并在此数据库上创建OData服务。
-— 更新 -—
此后,我尝试了Jen S提供的两个修复程序,现在得到错误:400错误的请求。
使用odata = verbose
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;odata=verbose;
Content-Length: 98
Host: scdb38:8888
{
"d":[
{
"Name":"Great White ","Food":"Surfers"
}
]
}
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 201
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed,16 Oct 2013 23:31:09 GMT
{"error":{"code":"1","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>An error occurred while processing this request.</Message></ExceptionInfo>"}}}
使用DataServiceVersion:1.0
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;
Content-Length: 98
Host: scdb38:8888
DataServiceVersion: 1.0;
{
"d":[
{
"Name":"Great White ","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>An error occurred while processing this request.</Message></ExceptionInfo>"}}}
使用WebServiceVerion:2.0
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;
Content-Length: 98
Host: scdb38:8888
DataServiceVersion: 2.0;
{
"d":[
{
"Name":"Great White ","Food":"Surfers"
}
]
}
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 201
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed,16 Oct 2013 23:38:23 GMT
{"error":{"code":"1","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>An error occurred while processing this request.</Message></ExceptionInfo>"}}}
我是否正在解决这个问题,这仅仅是JSON请求主体结构不正确的问题吗?我尝试了几次尝试都没有成功,但是使用XML进行发布是可行的。
----- 解决方案 ------
感谢Jen的帮助,使用json格式向OData服务发布对我来说很有效。
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;
Content-Length: 62
Host: scdb38:8888
DataServiceVersion: 1.0;
{
"Name":"Great White ","Food":"Surfers"
}
HTTP/1.1 201 Created
Cache-Control: no-cache
Content-Length: 298
Content-Type: application/json;odata=verbose;charset=utf-8
ETag: W/"X'00000000000007E7'"
Location: http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(4)
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu,17 Oct 2013 23:22:12 GMT
{"d":{"__metadata":{"id":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(4)","uri":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(4)","etag":"W/\"X'00000000000007E7'\"","Id":4,"RowVersion":"AAAAAAAAB+c=","Name":"Great White ","Food":"Surfers"}}
415不支持的媒体类型; Angular2到API
当我尝试从角度2发布到API我得到 – 415不支持的媒体类型.
Angular 2代码:
onSubmit(value: any) { // console.log(value.message); let headers = new Headers({ 'Content-Type': 'application/json'}); let options = new RequestOptions({ headers: headers }); let creds = 'statusukNown'; let body = JSON.stringify(creds); this.http.post('http://localhost:1318/api/ActionItem',creds) .subscribe( () => {console.log('Success')},err => {console.error(err)} ); }
我的控制器代码:
// POST api/actionitem [HttpPost] public ActionItem Post( [FromBody]string str)// _id,string _status) { ActionItem item = new ActionItem( 313,str); return item; }
当我将控制器代码更改为不从主体获取数据时,它可以工作但是引用NULL.
我的API调用截图:
请帮助&如果需要更多细节,请告诉我.
解决方法
this.http.post('http://localhost:1318/api/ActionItem',body,options).map(response => response.json()) .subscribe( () => {console.log('Success')} );
415不支持的媒体类型jQuery Ajax
我正在使用Maven Rest API。通过jQuery Ajax调用发出POST请求时,出现415错误。请看一下我的代码。
function getUserDetails() { var name = $("#name").val(); var mobile = $("#mobile").val(); var location = $("#location").val(); var email = $("#email").val(); var userDetails = { "name": name, "mobile": mobile, "email": email, "location": location }; return userDetails;}function createuser() { sendRequest("registerProcess", getUserDetails(), "post");}function sendRequest(url, input, method) { $.ajax({ url: url, async: false, data: JSON.stringify(input), error: function(response) { //displayResponseMessage(response); console.log("Error"); }, success: function(response) { console.log(" Successfull"); }, type: method, headers: { Accept: ''application/json;charset=utf-8'', contentType: ''application/json;charset=utf-8'' }, dataType: ''json'' });}
这是我的控制器。这UserForm
是POJO类别
@RequestMapping(value = "/registerProcess", method = RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)public String addUser(@RequestBody UserForm user) { System.out.println("Inside Controller" ); String email = user.getEmail(); String mobile = user.getMobile(); System.out.println("Details :" + email + mobile); return "welcome";}
提前致谢
答案1
小编典典提供标头''Content-Type'' : ''application/json''
,此处输入是可以直接在ajax请求中发送的对象data :input
关于Java HTTP 415 JSON不支持的媒体类型错误和java错误不支持发行版本5的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于415 Spring MVC和Rest Service不支持的媒体类型、415不支持的媒体类型-lightswitch 2012中的POST json到OData服务、415不支持的媒体类型; Angular2到API、415不支持的媒体类型jQuery Ajax等相关知识的信息别忘了在本站进行查找喔。
本文标签: