[英]REST: GET returns UnsupportedMediaType
I'm trying to GET
data from Server via ReST. 我正在尝试通过ReST从服务器
GET
数据。 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;
}
The Class is defined like this: 类的定义如下:
@Path("person")
@Component
public class PersonResource {
}
Now I'm trying to make a simple GET
on that resource: 现在,我正在尝试对该资源进行简单的
GET
:
http://localhost:8080/rest/person/1234567890
Accept: application/xml
And this is what my server responded: 这是我的服务器响应的内容:
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).
Why I'm getting this error when I'm using the GET
-Method. 为什么在使用
GET
-Method时出现此错误。 I think I've set the Accept-Header
correct and used the @Produces
in a way that's recommended. 我认为我已经正确设置了
Accept-Header
并以推荐的方式使用了@Produces
。 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
). (
POST & PUT
)。 Any ideas why I get the Error-Code 415
? 任何想法为什么我会收到
Error-Code 415
?
edited This is what my tomcat console says: 编辑这是我的tomcat控制台说什么:
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 only supports native types and String
s. Jersey仅支持本机类型和
String
。 Change your getPerson
function to the following and it should run: 将您的
getPerson
函数更改为以下内容,它将运行:
public PersonRepresentation getPerson(@PathParam("id") final long id) {
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 如果您真的希望传递对象,则可能会遇到另一个堆栈溢出问题: 使用Jersey将对象传递给REST Web服务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.