Java 如何将 JSON 对象从 POSTMAN 发送到 Restful webservices

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38971943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:47:28  来源:igfitidea点击:

How to send JSON Object from POSTMAN to Restful webservices

javamongodbweb-servicesrest

提问by dev777

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

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

My URL :

我的网址:

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

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;

JBWEB000236:servlet CXFServlet 的 Servlet.service() 抛出异常: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 ...

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

回答by Arnish gupta

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

第 1 步:打开邮递员,输入您的 api 网址并选择帖子类型。

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

第 2 步:转到 Headers button type("Content-Type") 第一个输入框并在第二个输入框中输入“application/json”它会给出自动完成建议

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

第 3 步:转到正文按钮并选择原始单选按钮,然后在相同原始右侧的下拉列表中选择 JSON (application/json)。

Step 4: type your json like student object

第 4 步:像 student 对象一样输入你的 json

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

check the attached sample example image

check the attached sample example image

回答by Rahamath

POSTMAN V5.2.0 Testing

POSTMAN V5.2.0 测试

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

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

JSON Data:

JSON 数据:

{"name":"John","firstName":"Smith","lastName":"MT","email":"[email protected]"}

Steps:

脚步:

  1. Add in Headers
  1. 添加标题
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....

  1. 通过单击粘贴上面的 JSON 数据BODY-> raw->JSON (application/json)

  2. 单击发送并查看 JSON/XML 格式的响应文本....

NOTE:

笔记:

  1. user refers in the URL your REST Spring controller
  1. 用户在 URL 中引用您的 REST Spring 控制器
@RestController

@RequestMapping("/user")
  1. register refers in the URL
  1. 注册在 URL 中引用
@RequestMapping(value = "/register", method = RequestMethod.POST, produces="application/json", consumes="application/json")

回答by GGEv

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

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

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