java - Sending JSON in GET Rest resource - curl and code have separate behavior -
I will start with: I am doing very wrong. And this is what I am doing.
I have created a REST resource for some searches and I expect a JSON data in the parameters:
@GET @ path ("/ device") @products (MediaTipp.APPLICATION_JSON) @Consume (MediaType.IPILICN_JSON) Public Response Search Communication (String Search jsonstring) {String Message = New Search Services (). Search (SearchSensString); Return Mill Response (message); // Checks the message for any error and sends back the feedback} // the end of the searchcontent () / I should not have written:
@Consumes Because it is a GET resource and it does not consume anything but my problem is the way to send JSON data to the Java code (GET resource) . I tried curl order which is capable of sending JSON data to this resource, but in no way is a Java code.
I tried to follow the curl order to send JSON data to:
curl -x gate-h "content-type: application / jason" -d '{"Keyword": "hello"}' http: // localhost: 8080 / search-test / rest / search / pre> and give it a proper JSON response to it and its work fine.
But if I am using a curl command without specifying any method (which should get a default http), I am responding to Tomcatquot 405 (method not allowed):
curl-d "{" keyword ":" hello "} 'http: // localhost: 8080 / search-test / rest / search or Java code Via:
HttpURLConnection urlConnection = (HttpURLConnection) New URL (urlString) .openConnection (); UrlConnection.setRequestProperty ("content-type", "app / jason"); urlConnection.setRequestMethod ("GET"); // This does not work The same 405 (method not permitted) response is received from Tomcat.
If I am sending a GET request using Java code, then I am not able to send JSON data as post method, and I have to be forced to use the name = value of the item and for that I need to accept my REST resource as a name / value pair .
It means something like this:
http: // l Ocalhost: 8080 / search-test / rest / search? Param = {"keyword": "permission"} If I'm doing something like a post:
@ POST @ path ("/ device ") @products (mediaptip.applex_jsan) @Consumes (MediaType.APPLICATION_JSON) public response searchContent {string search jsonstring} {string message = new search services). Search (SearchSensString); Return Mill Response (message); // Checks the message for any error and sends back the feedback} // the end of the searchcontent () / I also send JSON data both by java code and curl order I am able to:
curl -X post-H "content-type: application / jason" -d '{"keyword": "hello"}' http: // local host: 8080 / Search-test / rest / search or via java code:
HttpURL connection URL connection = (HttpURL connection) new urlString .openConnection (); UrlConnection.setRequestMethod ("post"); //works fine. UrlConnection.setRequestProperty ("content-type", "app / jason"); UrlConnection.setDoOutput (true); Where's the problem? Why can not I send it to the code, but from curl? Is there any other way to send JSON data other than name = value pair to GET resource?
HttpURL does not allow requests with connection units and requests before sending from server Will erase the unit from Therefore, I strongly recommend to avoid it, even if you use a different Java HTTP client library that allows you to do this, your users will also have similar issues (plus web cache and proxy more problems Can be added) will be likely to run in.
Comments
Post a Comment