我正在尝试将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;
@Override
public String insertData(JSONObject jsonlist) throws UnknownHostException {
System.out.println(jsonlist);
insertDataDao.insertData(jsonlist);
return "SUCCESS";
}
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;
我无法解决此问题。任何人都可以帮我解决这个问题......
答案 0 :(得分:8)
网址:http://localhost:8080/mail/user/register/
JSON数据:
{"name":"John","firstName":"Smith","lastName":"MT","email":"johnsmt@yahoo.com"}
步骤:
key: content-type
value: application/json
点击BODY
- &gt;粘贴上面的JSON数据raw
- &gt; JSON (application/json)
点击发送并查看JSON / XML中的响应文本....
注意:强>
@RestController
@RequestMapping("/user")
@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>