简体   繁体   English

REST:GET返回UnsupportedMediaType

[英]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.

相关问题 Java-Spring:REST GET 请求在正文中返回错误 - Java-Spring: REST GET request returns something wrong in body SugarCRM REST API:get_relationships返回空豆 - SugarCRM REST API: get_relationships returns empty beans Java和REST Web服务-GET方法返回JSONObject [“…”]不是JSONObject - Java and REST web service - GET method returns JSONObject[“…”] is not a JSONObject Rest template.exchange 在对 dot.net 核心中运行 REST 服务的服务器执行 GET 请求时将响应返回为垃圾 - Rest template.exchange returns response as junk while doing a GET request to a server running REST service in dot net core 如何获得Openstack json模式以获取其ReST API返回的响应? - How do I get Openstack json schema for the responses it returns for its ReST API? 公开单个 REST 端点 (GET),该端点返回父 object 以及与之关联的所有子对象 - Expose single REST endpoint (GET) which returns a parent object and all the child objects associated with it Spring Rest RequestMethod.GET 返回 400 Bad Request for @RequestParam required=true 丢失时 - Spring Rest RequestMethod.GET returns 400 Bad Request for @RequestParam required=true when missing 定义返回列表的REST端点 - Define REST endpoint that returns a List Java Rest Api返回404 - Java Rest Api returns 404 Hazelcast Rest API不返回任何内容 - Hazelcast Rest API returns no content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM