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 is as below:
//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"/>
我检查了代码,但没有发现任何线索,导致此POST类型为空白响应,而它的GET类型工作正常。我虽然它应该适用于POST类型,因为代码规范对于这两种类型都是正确的。请指出那里的任何问题。在此先感谢使用POST显示空白响应的REST API
谢谢,我可以使用GET,而使用@FormParam标注基于参数REST API?由于我在使用GET时出错。 – user2267023
@ user2267023,不需要POST和PUT,您可以在需要时使用表单参数。对于GET,只有'@ QueryParam'可以工作。快速链接:https://stackoverflow.com/questions/29789801/formparam-does-not-work-with-get-method-resteasy。 –
我们可以使用ObjectMapper类来实现这个使用POST?请任何想法。 – user2267023