1

While working on REST API, I am using POST method to fetch data from Mongo DB using @FormParam annotation. When I use GET type, then it returns the response in JSON and while changing the method from GET to POST, I am getting blank response.

The code:

//GetResponse.java
//
@Path("/Location")
public class GetProjectLocationResponse {
   @POST
    @Path("/State") 
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Object[] addBuild2( 
           @FormParam("Country") String Country,  
           @FormParam("State") String State) throws UnknownHostException, JSONException
    { 
        System.out.println("ïnside Location");
        Location loc = new Location();
        List<DBObject> basicDBList=(List<DBObject>) loc.getState2(Country, State); //calling state method
        return basicDBList.toArray();
    }
        
//Location.java
//This defines the list of available zipcpdes on the basis on parameter 'Country' and 'States'.
    
    public List<DBObject> getState2(String Country, String State) throws UnknownHostException {
        DB db=ConnectToDB.getConnection();
        DBCollection collection = db.getCollection("location");
        BasicDBObject obj = new BasicDBObject();
        obj.put("Country",Country);
        obj.put("State",State);
        BasicDBObject fields = new BasicDBObject();
        fields.put("_id", 0);
        DBCursor cursor = collection.find(obj, fields);
        List<DBObject> obj1 =cursor.toArray();
        System.out.println(""+obj1);
        return obj1;
    }
}
    
    //index.html
//This file includes parameters 'country' and 'states' to return the JSON response.
<form action="rest/Location/State" method="post">  
Enter Country:<input type="text" name="Country"/><br/><br/>  
Enter State:<input type="text" name="State"/><br/><br/>  
<input type="submit" value="submit"/>  
    

I have checked the code but did not find any clue what is wrong in this that causing this blank response for POST type, while its working fine for GET type. I though it should have worked for POST type as the code specification is correct for both type. Please specify any issue there. Thanks in advance

James Z
  • 12,209
  • 10
  • 24
  • 44
user2267023
  • 37
  • 2
  • 8

1 Answers1

0

You are using POST for a different and wrong purpose here.

Use GET for retrieving data and POST for creation of the required entity. A typical response from a POST call would be 201 - Created and the UI should not expect any response from the POST call.

HTTP POST was not specified to return data and do not expect it to return.

Similar question : Can I use POST method to get data and GET method to post data?

Karthik R
  • 5,523
  • 2
  • 18
  • 30
  • Thanks, Can I use GET while using @FormParam annotation for parameter based REST API? As I am getting error while using GET. – user2267023 Sep 05 '17 at 06:04
  • @user2267023, No. POST and PUT allows you to have form parameters should you need them. For GET only `@QueryParam` would work. Quick Link: https://stackoverflow.com/questions/29789801/formparam-does-not-work-with-get-method-resteasy . – Karthik R Sep 05 '17 at 06:09
  • Can we use ObjectMapper class to achieve this using POST? any idea please. – user2267023 Sep 06 '17 at 13:24
  • You can pass the request body to the POST, just not the FormParam. Feel free to explore. Pass the HttpServletRequest request or request body and use Object Mapper to transform your request and proceed forward. Not sure if you just use JAX-RS implementation. If you are using Spring REST, feel free to proceed with `@RequestBody` – Karthik R Sep 07 '17 at 04:30
  • Thanks, I am using JAX-RS implementation not Spring RESt, so that solution is not possible ? – user2267023 Sep 07 '17 at 04:48
  • Possible. Use the request body to transform the JSON to entity using ObjectMapper. Reference : https://stackoverflow.com/questions/16149507/obtaining-raw-request-body-in-jax-rs-resource-method – Karthik R Sep 07 '17 at 05:14