gpt4 book ai didi

java - REST:GET 返回不支持的媒体类型

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:42 24 4
gpt4 key购买 nike

我正在尝试通过 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;
}

类的定义如下:

@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 。经过谷歌搜索后,我发现了一些“解决方案”:您必须设置请求的内容类型。但不是只有当我有 request-body 时才有必要吗? (POST 和 PUT)。有什么想法为什么我会收到错误代码 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
*/* ->

最佳答案

Jersey 仅支持 native 类型和String。将您的 getPerson 函数更改为以下内容,它应该运行:

public PersonRepresentation getPerson(@PathParam("id") final long id) {

如果您确实希望传递一个对象,另一个 Stack Overflow 问题可能有您正在寻找的内容:Passing an object to a REST Web Service using Jersey

关于java - REST:GET 返回不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com