如何将JSON对象从POSTMAN发送到Restful Web服务

时间:2016-08-16 10:01:52

标签: java mongodb web-services rest

我正在尝试将json从POSTMAN发送到RESTful webservice。我已经按照本教程url通过POSTMAN发送json。

我的网址:

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

我的服务方法:

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

我的Impl:

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

我的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";

    }

但我面对以下例外:

 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;

我无法解决此问题。任何人都可以帮我解决这个问题......

2 个答案:

答案 0 :(得分:8)

POSTMAN V5.2.0测试

网址:http://localhost:8080/mail/user/register/

JSON数据:

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

步骤:

  1. 添加标题
  2. key: content-type
    value: application/json
    
    1. 点击BODY - &gt;粘贴上面的JSON数据raw - &gt; JSON (application/json)

    2. 点击发送并查看JSON / XML中的响应文本....

    3. 注意:

      1. 用户在您的REST Spring控制器的URL中引用
      2. @RestController
        
        @RequestMapping("/user")
        
        1. 注册表引用URL
        2. @RequestMapping(value = "/register", method = RequestMethod.POST, produces="application/json", consumes="application/json")
          

答案 1 :(得分:0)

我遇到了同样的问题,首先导入了jersey.json依赖项,然后将其添加到web.xml

<init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>