我使用jax-rs和jersey创建了一个REST Web服务,它应该在POST请求中使用JSON.我的Web服务类看起来像这样:
@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 \n======================="); 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(如果需要)
SnapScan-Webhook com.sun.jersey.spi.container.servlet.ServletContainer com.sun.jersey.config.property.packages za.co.lancet.service com.sun.jersey.api.json.POJOMappingFeature true 1 SnapScan-Webhook /*
我正在使用RESTClient,好吧,休息客户端...这是我发送的截图:
当我发送它时,我得到415 Unsupported Media Type错误.有谁知道为什么?
正如其他人所指出的那样,你错过了正确的标题.添加Content-Type: application/json
到"标题":
您需要发送请求标头Content-Type: application/json
.似乎REST-Client不会自动为您添加此标头.