21

I'm trying to send json from POSTMAN to RESTful webservice. I had followed this tutorial url for sending json through POSTMAN.

My URL :

http://localhost:8080/myWebService/rest/dataInsert/insert

My Service method:

@POST
    @Path("/insert")
    @Consumes(MediaType.APPLICATION_JSON)
    public String insertData(JSONObject jsonlist) throws UnknownHostException;

My Impl:

@Override
    public String insertData(JSONObject jsonlist) throws UnknownHostException {
        System.out.println(jsonlist);
        insertDataDao.insertData(jsonlist);
        return "SUCCESS";
    }

My DAO:

public  String insertData(JSONObject jsonlist) throws UnknownHostException{
        System.out.println(jsonlist);
        MongoConnection mongoconnection = new MongoConnection();
        MongoClient mongoclient = mongoconnection.getMongoClient();

        MongoDatabase db = mongoclient.getDatabase("mydb");
        MongoCollection<Document> col = db.getCollection("col");

        String jsonString = jsonlist.toString();
        System.out.println(jsonString);

        Document doc = Document.parse(jsonString);
         col.insertOne(doc);
        System.out.println("Inserted Successfully !!!");
        return "SUCCESS";

    }

But I'm facing the below Exception:

JBWEB000236: Servlet.service() for servlet CXFServlet threw exception: java.lang.NoSuchMethodError: javax.ws.rs.InternalServerErrorException.validate(Ljavax/ws/rs/core/Response;Ljavax/ws/rs/core/Response$Status;)Ljavax/ws/rs/core/Response;

I'm unable to fix this issue. Can anyone please help me out regarding this ...

Sana
  • 360
  • 3
  • 13
dev777
  • 999
  • 6
  • 17
  • 34

3 Answers3

43

Step 1: open postman type your api url and select post type.

Step 2: goto Headers button type("Content-Type") first input box and type "application/json" in second input box it gives auto-complete suggestion

Step 3: goto Body button and choose raw radio button and choose JSON (application/json) in drop down in same raw right side.

Step 4: type your json like student object

{
  "name": "dummy",
  "marks": "26"
}

check the attached sample example image

Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49
Arnish gupta
  • 461
  • 5
  • 7
  • Yes. I did the same thing. But I'm unable to send this json data to webservice. OnClick of send button in POSTMAN, I'm facing the issue which is mentioned above – dev777 Aug 16 '16 at 12:22
11

POSTMAN V5.2.0 Testing

URL: http://localhost:8080/mail/user/register/

JSON Data:

{"name":"John","firstName":"Smith","lastName":"MT","email":"johnsmt@yahoo.com"}

Steps:

  1. Add in Headers
key: content-type
value: application/json
  1. Paste above JSON data by click BODY -> raw -> JSON (application/json)

  2. Click send and see the response text in JSON/XML....

NOTE:

  1. user refers in the URL your REST Spring controller
@RestController

@RequestMapping("/user")
  1. register refers in the URL
@RequestMapping(value = "/register", method = RequestMethod.POST, produces="application/json", consumes="application/json")
General Failure
  • 2,421
  • 4
  • 23
  • 49
Rahamath
  • 491
  • 6
  • 4
1

I had the same problem which was solved by importing the jersey.json dependency first and then adding this in web.xml

<init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
GGEv
  • 843
  • 9
  • 23