REST Webservice returning 415 - Unsupported Media Type(REST Web 服务返回 415 - 不支持的媒体类型)
问题描述
我使用 jax-rs 和 jersey 创建了一个 REST Web 服务,它应该在 POST 请求上使用 JSON.我的网络服务类如下所示:
I've created a REST webservice using jax-rs and jersey that is supposed to consume JSON on a POST request. My web service class looks like this:
@Path("/webhookservice")
public class Webhook {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response readData (Song song) {
// Prints out the song info
System.out.println("SONG INFO
=======================");
System.out.println("songname: " + song.getSongname());
System.out.println("artist: " + song.getArtist());
// Repsonse with a HTTP 200 OK
Response response = Response.status(200).build();
return response;
}
}
我的歌曲课:
public class Song {
private String songname;
private String artist;
public String getSongname () { return this.songname; }
public String getArtist () { return this.artist; }
public void setSongname (String songname) { this.songname = songname; }
public void setArtist (String artist) { this.artist = artist; }
}
我的 web.xml(如果需要)
My web.xml (if needed)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>SnapScan-Webhook</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>za.co.lancet.service</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SnapScan-Webhook</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我正在使用 RESTClient 一点,好吧,休息客户端...这是我的截图发送:
I'm using RESTClient a little, well, rest client... Here's a screenshot of what I'm sending:
当我发送它时,我收到 415 Unsupported Media Type 错误.有人知道为什么吗?
When I send that off, I get the 415 Unsupported Media Type error. Anybody have an idea why?
推荐答案
需要发送request-header Content-Type: application/json
.似乎 REST-Client 不会为您自动添加此标头.
You need to send the request-header Content-Type: application/json
. Seems like REST-Client does not add this header automatically for you.
这篇关于REST Web 服务返回 415 - 不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:REST Web 服务返回 415 - 不支持的媒体类型
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01