2

I am pretty new to REST based web services. I am trying to call a small REST WS that I created the start of which looks as below

    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;
    }   

}

Whenever I call the POST service same from Chrome postman using form-data

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

password admin

POSTMAN call screenshot

I get below response

<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>
Biraja
  • 83
  • 6

3 Answers3

1
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })

Try to use one of them, for sample only MediaType.APPLICATION_JSON

For beginning with Rest Service this is a top tutorial: http://crunchify.com/how-to-build-restful-service-with-java-using-jax-rs-and-jersey/

Mykola
  • 3,343
  • 6
  • 23
  • 39
ElMariachi25
  • 186
  • 4
  • 14
  • 1
    Thanks for the quick response. I tried your suggestion but still see the same error. I tried by running the server in debug. The control does not get into the 'authenticateUser' method. So I am doubting that the issue is more related to **@Consumes** and **@FormParam**. Thanks for the REST start-up link – Biraja Jan 25 '16 at 16:39
1

You need to set the Headers correctly at Postman.

At your request tab, press Headers and set a new variable like this.

  • Content-Type -> application/json
kunpapa
  • 367
  • 1
  • 9
  • Change your consume declaration to: - **MediaType.APPLICATION_JSON** . `@Consumes(MediaType.MediaType.APPLICATION_JSON)` – kunpapa Jan 25 '16 at 16:57
  • @BondCode Also, did you tryed to send JSON answer instead of form-data? Im seeing your img (http://i.stack.imgur.com/IPn6h.png). Press the button raw and write the next JSON: `{"user":"administrator", "password" : "admin"}` – kunpapa Jan 25 '16 at 17:12
  • Please let me know how do I access the JSON in method. I was doing the same with form parameters as below public String authenticateUser(@FormParam("user") String user, @FormParam("password") String pwd) – Biraja Jan 26 '16 at 12:41
  • Check this thread: - http://stackoverflow.com/questions/14600510/how-to-send-and-receive-json-data-from-a-restful-webservice-using-jersey-api – kunpapa Jan 26 '16 at 13:00
  • Thanks kunpapa ... big help – Biraja Jan 27 '16 at 05:01
0

Thanks everyone for their quick responses ... The JSON type paremeter passing works. In case the original approach has to be debugged I did the following change in method parameter signature and it worked

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

previously it was

public String authenticateUser(@FormParam("user") String user, @FormParam("password") String pwd)
Biraja
  • 83
  • 6