如何直接在浏览器URL中发送json对象?

时间:2015-02-25 08:09:34

标签: java

我们的客户端将根据我想要更新数据库的数据,使用我们的servlet上的json对象发送请求。

json对象如下:

{"person":{"employment":{"id":"123","department":"developer","Company":"XYZ","sal":"10000"},"fName":"ABC","mName":"PQR","lName":"XYZ","email":"asd@asd.com","address":"mumbai west"}

我想通过直接在浏览器网址上发送json对象来测试这一点,该对象包含超过256个字符。

我试过了: -

https://integration.com/details/Notification?req={"person":{"employment":{"id":"123","department":"developer","Company":"XYZ","sal":"10000"},"fName":"ABC","mName":"PQR","lName":"XYZ","email":"asd@asd.com","address":"mumbai west"}

但它给了我以下例外:

  

org.codehaus.jackson.JsonParseException:意外字符(' o'(代码111)):预期有效值(数字,字符串,数组,对象,' true', ' false'或' null')    在[来源:java.io.StringReader@3e3a5a9c; line:1,column:2]           at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)           在org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)           在org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)           at org.codehaus.jackson.impl.ReaderBasedParser._handleUnexpectedValue(ReaderBasedParser.java:1198)           在org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:485)           at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2770)           at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)           在org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)           在payment.EmaxCBNotification.doService(EmaxCBNotification.java:72)           在payment.EmaxCBNotification.doGet(EmaxCBNotification.java:42)           在javax.servlet.http.HttpServlet.service(HttpServlet.java:617)           在javax.servlet.http.HttpServlet.service(HttpServlet.java:723)           在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)           在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)           at com.directi.pg.filters.ESAPITransactionFilter.doFilter(ESAPITransactionFilter.java:276)           在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)           在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)           at com.directi.pg.filters.UTF8Filter.doFilter(UTF8Filter.java:31)           在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)           在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)           在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)           在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)           在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)           在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)           在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)           在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)           在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)           在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:606)           在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489)           在java.lang.Thread.run(Thread.java:662)

请帮助我!

我应该使用HTTP POST请求,它会在浏览器上以上面定义的格式命中网址吗?

3 个答案:

答案 0 :(得分:0)

使用GET检索数据并使用PUT替换(更新)数据。永远不要使用GET来更新数据 - 永远不要!

Representational state transfer from en.wikipedia.org

答案 1 :(得分:0)

如果您使用POST更新数据,可以使用CURL发布请求&测试

   curl -v -H "Content-Type: application/json" -X POST \
    -d '{"name":"your name","phonenumber":"111-111"}' 
   URL

它在这方面做得很好 POST: sending a post request in a url itself

答案 2 :(得分:0)

是的,您当然可以在URL Querystring中传递JSON。您必须首先对JSON字符串进行URLencode。正如@dmn所说,由于GET的大小限制,数据可能更好地通过POST传递。