简体   繁体   English

静态Web服务中的Application / JSON是不受支持的媒体类型

[英]Application/JSON is Unsupported Media Type in restful webservice

I am trying to create a restful-jersey webservice. 我正在尝试创建一个restful-jersey Web服务。 I have to pass JSON object to the webservice. 我必须将JSON对象传递给Web服务。

    @POST
    @Path("/saveVehicleTrackingData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public String saveVehicleTrackingData(VehicleTracking vehicleTracking) {
        return vehicleTracking.toString();
    }

When I try try to make request to the service, it says HTTP Status 415 - Unsupported Media Type. 当我尝试向服务发出请求时,它显示HTTP状态415-不支持的媒体类型。 Please help. 请帮忙。 Also, what should be the type of single argument of the method saveVehicleTrackingData . 另外,方法saveVehicleTrackingData的单个参数的类型应该是什么。

PS: I am using POSTMAN to make http request. PS:我正在使用POSTMAN发出http请求。 http://goo.gl/vwXNXQ http://goo.gl/vwXNXQ

在此处输入图片说明

UPDATE : As pointed out by peeskillet, the missing thing here is JSON Provider. 更新:正如peeskillet指出的,这里缺少的是JSON Provider。 The next challenge that I have is, how to integrate the JSON Provider in my project. 我面临的下一个挑战是如何在项目中集成JSON Provider。 After researching a little, I found FasterXML jackson as one of the JSON provider. 经过研究后,我发现FasterXML jackson是JSON提供程序之一。

For your first question: 对于第一个问题:

What kind of hosting (Tomcat, GlassFish or whatever...) are you using, and what is the Java version of the host? 您正在使用哪种托管(Tomcat,GlassFish或其他...),以及托管的Java版本是什么? I know from experience that different version of Tomcat in combination with Jersey sometimes gives problems with @Consumes(MediaType.APPLICATION_JSON) . 从经验中我知道,不同版本的Tomcat与Jersey结合使用有时会给@Consumes(MediaType.APPLICATION_JSON)带来问题。

About your second question: 关于第二个问题:

You want to build a RESTful webservice, the right name for a path should be; 您要构建RESTful Web服务,路径的正确名称应为; VehicleTrackingData (or whatever you like). VehicleTrackingData (或您喜欢的任何数据)。 This path will work (request/response) with the known HTTP verbs; 该路径将与已知的HTTP动词一起工作(请求/响应); GET, POST, PUT or DELETE. GET,POST,PUT或DELETE。

在此处输入图片说明

This image is just for reference. 该图像仅供参考。 I just had it from another post. 我刚从另一篇文章中得到它。

Basically when you use raw , it will default to text/plain . 基本上,当您使用raw时 ,它将默认为text/plain The JSON in the drop down, is simply to select syntax highlighting. 下拉列表中的JSON只是选择语法突出显示。 You still need to set the Content-Type header to application/json . 您仍然需要将Content-Type标头设置为application/json You can click on the Headers button and add it. 您可以单击“ 标题”按钮并将其添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM