【问题标题】:Consume REST web service using POST call returns 415使用 POST 调用使用 REST Web 服务返回 415
【发布时间】:2016-05-02 00:54:45
【问题描述】:

我对基于 REST 的 Web 服务非常陌生。我正在尝试调用我创建的小型 REST WS,其开头如下所示

    package webServices;

import java.net.UnknownHostException;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.json.JSONException;
import org.json.JSONObject;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;



@Path("/login")
public class LoginService {



    @Path("/isUp")
    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })

    public String checkServiceStatus(){

        return "up and running";

    }

    @Path("/authenticate")
    @POST
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String authenticateUser(@FormParam("user") String user, @FormParam("password") String pwd){


        DB db;
        DBCollection coll;

        MongoClient mongoClient;
        String loginResponse="user does not exist";
        try {
            mongoClient = new MongoClient( "localhost" , 27017 );

            db = mongoClient.getDB( "Hackathon" );
            coll = db.getCollection("users");

            BasicDBObject filter = new BasicDBObject();
            filter.put("user", user);
            BasicDBObject selectField = new BasicDBObject();
            selectField.put("password", 1);
            selectField.put("_id", 0);

            DBCursor cursor = coll.find(filter, selectField);
            String jsonString = cursor.next().toString();
            JSONObject json = new JSONObject(jsonString);
            String password = json.getString(user);
            System.out.println("password "+password);

            if(password.equals(pwd)){
                loginResponse="success";
                System.out.println("success");
            }else{
                loginResponse="failure";
                System.out.println("failure");
            }


        } catch (UnknownHostException e) {

            e.printStackTrace();
        } catch (JSONException e) {

            e.printStackTrace();
        }


        return loginResponse;
    }   

}

每当我使用表单数据从 Chrome 邮递员那里调用 POST 服务时

http://localhost:8080/HackDataEngine/login/authenticate
Content-Type application/json
user admin

password admin

POSTMAN call screenshot

我得到以下回应

<html>
    <head>
        <title>Apache Tomcat/7.0.67 - Error report</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
        </style>
    </head>
    <body>
        <h1>HTTP Status 415 - Unsupported Media Type</h1>
        <HR size="1" noshade="noshade">
            <p>
                <b>type</b> Status report
            </p>
            <p>
                <b>message</b>
                <u>Unsupported Media Type</u>
            </p>
            <p>
                <b>description</b>
                <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.</u>
            </p>
            <HR size="1" noshade="noshade">
                <h3>Apache Tomcat/7.0.67</h3>
            </body>
        </html>

【问题讨论】:

    标签: java web-services rest


    【解决方案1】:
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
    

    尝试使用其中之一,仅供示例MediaType.APPLICATION_JSON

    从 Rest Service 开始,这是一个顶级教程: http://crunchify.com/how-to-build-restful-service-with-java-using-jax-rs-and-jersey/

    【讨论】:

    • 感谢您的快速回复。我尝试了您的建议,但仍然看到相同的错误。我尝试在调试中运行服务器。控件没有进入 'authenticateUser' 方法。所以我怀疑这个问题是否与 @Consumes@FormParam 更相关。感谢 REST 启动链接
    【解决方案2】:

    您需要在 Postman 正确设置 Headers

    在您的请求选项卡上,按标题并像这样设置一个新变量。

    • 内容类型 -> 应用程序/json

    【讨论】:

    • 将您的消费声明更改为: - MediaType.APPLICATION_JSON@Consumes(MediaType.MediaType.APPLICATION_JSON)
    • @BondCode 另外,您是否尝试发送 JSON 答案而不是表单数据?我看到你的 img (i.stack.imgur.com/IPn6h.png)。按下按钮 raw 并写入下一个 JSON:{"user":"administrator", "password" : "admin"}
    • 请告诉我如何访问 JSON in 方法。我对表单参数做同样的事情,如下 public String authenticateUser(@FormParam("user") String user, @FormParam("password") String pwd)
    • 谢谢kunpapa ...大帮助
    【解决方案3】:

    感谢大家的快速响应...... JSON 类型参数传递有效。如果必须调试原始方法,我对方法参数签名进行了以下更改并且它起作用了

    public String authenticateUser(@FormParam("user") String user, @FormParam("password") String password)
    

    以前是

    public String authenticateUser(@FormParam("user") String user, @FormParam("password") String pwd)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多