我正在尝试通过ReST从服务器GET
获取数据。我的服务器端资源方法如下所示:
@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;
}
Class的定义如下:
@Path("person")
@Component
public class PersonResource {
}
现在我正在尝试在该资源上创建一个简单的GET
:
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
。谷歌搜索后,我发现一些“解决方案”说:你必须设置你的请求的内容类型。但是,如果我有一个请求体,是不是只有必要? (POST & PUT
)。我有什么想法得到Error-Code 415
?
编辑 这就是我的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
*/* ->
答案 0 :(得分:1)
Jersey仅支持原生类型和String
。将您的getPerson
功能更改为以下内容并运行:
public PersonRepresentation getPerson(@PathParam("id") final long id) {
如果你真的希望传入一个对象,另一个Stack Overflow问题可能会有你想要的东西:Passing an object to a REST Web Service using Jersey