我实际上是 REST WS 的新手,但我真的不明白这一点
415 Unsupported Media Type
。
我正在 Firefox 上使用 Poster 测试我的 REST,
GET
对我来说效果很好,POST
(当它是 application/xml
时),但是当我尝试 application/json
时,它根本不会到达 WS ,服务器拒绝它。
这是我的网址:http://localhost:8081/RestDemo/services/customers/add
这是
JSON
我正在发送:{"name": "test1", "address" :"test2"}
这是
XML
我正在发送:
<customer>
<name>test1</name>
<address>test2</address>
</customer>
这是我的资源类:
@Produces("application/xml")
@Path("customers")
@Singleton
@XmlRootElement(name = "customers")
public class CustomerResource {
private TreeMap<Integer, Customer> customerMap = new TreeMap<Integer, Customer>();
public CustomerResource() {
// hardcode a single customer into the database for demonstration
// purposes
Customer customer = new Customer();
customer.setName("Harold Abernathy");
customer.setAddress("Sheffield, UK");
addCustomer(customer);
}
@GET
@XmlElement(name = "customer")
public List<Customer> getCustomers() {
List<Customer> customers = new ArrayList<Customer>();
customers.addAll(customerMap.values());
return customers;
}
@GET
@Path("/{id}")
@Produces("application/json")
public String getCustomer(@PathParam("id") int cId) {
Customer customer = customerMap.get(cId);
return "{\"name\": \" " + customer.getName() + " \", \"address\": \"" + customer.getAddress() + "\"}";
}
@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public String addCustomer(Customer customer) {
//insert
int id = customerMap.size();
customer.setId(id);
customerMap.put(id, customer);
//get inserted
Customer result = customerMap.get(id);
return "{\"id\": \" " + result.getId() + " \", \"name\": \" " + result.getName() + " \", \"address\": \"" + result.getAddress() + "\"}";
}
}
编辑1:
这是我的客户课程:
@XmlRootElement
public class Customer implements Serializable {
private int id;
private String name;
private String address;
public Customer() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
添加
Content-Type: application/json
和 Accept: application/json
在 REST 客户端标头部分
问题出在 Customer bean 的反序列化中。正如 Daniel 所写,您的程序知道如何使用 XML 和 JAXB 来执行此操作,但很可能不知道如何使用 JSON 执行此操作。
这里有一个 Resteasy/Jackson 的例子 http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/
与泽西岛相同: http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/
以防万一这对其他人有帮助,这是我的轶事:
我发现这个线程是由于我在使用 Postman 将测试数据发送到我的 RESTEasy 服务器时遇到的问题,在重大代码更改之后,我除了 415 不支持的媒体类型错误之外什么也没有得到。
长话短说,我把所有东西都撕掉了,最终我尝试运行我知道有效的简单文件上传示例;事实并非如此。就在那时我意识到问题出在我的邮递员请求上。我通常不会发送任何特殊标头,但在之前的测试中我添加了一个“Content-Type”:“application/json”标头。当然,我试图上传“multipart/form-data”。删除它解决了我的问题。
寓意:在毁掉你的世界之前检查你的标题。 ;)
就我而言,缺少以下依赖项:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
我遇到了这个问题,发现问题是我没有注册 JacksonFeature 类:
// Create JAX-RS application.
final Application application = new ResourceConfig()
...
.register(JacksonFeature.class);
如果不这样做,您的应用程序将不知道如何将 JSON 转换为 java 对象。
https://jersey.java.net/documentation/latest/media.html#json.jackson
我也遇到了同样的问题:
curl -v -H“内容类型:application/json”-X PUT -d '{“姓名”:“json”,“姓氏”:“gson”,“已婚”:true,“年龄”:32, “工资”:123,“hasCar”:true,“childs”:[“serkan”,“volkan”,“aybars”]}'XXXXXX/ponyo/UserModel/json
* About to connect() to localhost port 8081 (#0)
* Trying ::1...
* Connection refused
* Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 8081 (#0)
> PUT /ponyo/UserModel/json HTTP/1.1
> User-Agent: curl/7.28.1
> Host: localhost:8081
> Accept: */*
> Content-Type: application/json
> Content-Length: 121
>
* upload completely sent off: 121 out of 121 bytes
< HTTP/1.1 415 Unsupported Media Type
< Content-Type: text/html; charset=iso-8859-1
< Date: Wed, 09 Apr 2014 13:55:43 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
* Closing connection #0
我通过将依赖项添加到 pom.xml 来解决它,如下所示。请尝试一下。
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.98</version>
</dependency>
我也遇到同样的问题。 事实上,请求并没有到达球衣标注的方法。 我通过添加注释解决了问题:@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 注释 @Consumes("/") 不起作用!
@Path("/"+PropertiesHabilitation.KEY_EstNouvelleGH)
@POST
//@Consumes("*/*")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void get__EstNouvelleGH( @Context HttpServletResponse response) {
...
}
不要在方法中返回字符串,但客户会自行反对它并让 JAXB 负责反/序列化。
我遇到了同样的问题,因为它在使用 json 进行 post 调用时给出了 error-415-unsupported-media-type,而我已经有了
@Consumes(MediaType.APPLICATION_JSON)
以及使用 Jersey 2.0+ 的restful Web 服务中的请求标头
Accept:application/json
Content-Type:application/json
我通过向我的项目添加以下依赖项解决了这个问题
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25</version>
</dependency>
这会将以下 jar 添加到您的库中:
我希望它对其他人也适用,就像对我一样。
当您将 JSON 与 POST/PUT 等一起使用时,只需将内容类型更改为 application/json...
我在邮递员中遇到了同样的问题,我在标题部分选择接受并提供“application/json”值。所以我取消选中并选择 Content-Type 并将值提供为“application/json”。成功了!
如果所有解决方案都不起作用并且您正在使用 JAXB,那么您需要使用
@XmlRootElement
注释您的类以避免 415 unsupported media type
我通过添加此依赖项解决了
implementation ("io.quarkus:quarkus-resteasy-jsonb")
我在邮递员中使用时遇到了类似的问题。 对于标头部分下的 POST 请求,将这些添加为
键:值对
内容类型:application/json 接受:application/json
我希望它会起作用。