REST:GET 返回 UnsupportedMediaType [英] REST: GET returns UnsupportedMediaType
问题描述
我正在尝试通过 ReST 从服务器 GET
数据.我的服务器端资源方法如下所示:
I'm trying to GET
data from Server via ReST. My server-side resource method looks like this:
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public PersonRepresentation getPerson(@PathParam("id") @Nonnull final Long id) {
// collect data
// and return representation
return personRepresentation;
}
类定义如下:
@Path("person")
@Component
public class PersonResource {
}
现在我正在尝试对该资源进行简单的GET
:
Now I'm trying to make a simple GET
on that resource:
http://localhost:8080/rest/person/1234567890
Accept: application/xml
这是我的服务器响应:
HTTP Status 415 - Unsupported Media Type
type: Status report
message: Unsupported Media Type
description: The server refused this request because the request entity is in a format
not supported by the requested resource for the requested method (Unsupported Media Type).
为什么我在使用 GET
方法时会收到此错误.我想我已经正确设置了 Accept-Header
并以推荐的方式使用了 @Produces
.在谷歌搜索之后,我发现了一些解决方案"说:您必须设置请求的内容类型.但不是只有当我有一个 request-body 时才需要吗?(POST & PUT
).任何想法为什么我得到 Error-Code 415
?
Why I'm getting this error when I'm using the GET
-Method. I think I've set the Accept-Header
correct and used the @Produces
in a way that's recommended. After googling around, I've found some "solutions" that say: you have to set the content-type of your request. But isn't it only necessary if I have a request-body ? (POST & PUT
). Any ideas why I get the Error-Code 415
?
已编辑这是我的 tomcat 控制台所说的:
edited This is what my tomcat console says:
SEVERE: A message body reader for Java class java.lang.Long, and Java type class java.lang.Long, and MIME media type application/octet-stream was not found
Feb 25, 2014 3:00:24 PM com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: The registered message body readers compatible with the MIME media type are:
application/octet-stream ->
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider
*/* ->
推荐答案
Jersey 仅支持本机类型和 String
s.将您的 getPerson
函数更改为以下内容,它应该会运行:
Jersey only supports native types and String
s. Change your getPerson
function to the following and it should run:
public PersonRepresentation getPerson(@PathParam("id") final long id) {
如果您真的希望传入一个对象,另一个 Stack Overflow 问题可能有您想要的:将对象传递给使用 Jersey 的 REST Web 服务
If you really wish to pass in an object, another Stack Overflow question may have what you're looking for: Passing an object to a REST Web Service using Jersey
这篇关于REST:GET 返回 UnsupportedMediaType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!