3

I am relatively new to JAVA and am trying to build a back-end application which will call another server where the data is present.

I have to do GET, POST, PUT and PATCH(can live without PATCH) operations to an external server in the intranet. I can hit the exposed service using postman app (using custom header)but want to do the similar action from my Java code, get the output in my Tomcat Server.

I have limitations in modifying dependencies to the POM file or adding jars to the library, so am looking for a solution other than Jersey.

DXBKing
  • 195
  • 1
  • 2
  • 10

3 Answers3

3

You can use HttpURLConnection (which extends URLConnection) to handle the http requests and these classes are part of JDK itself so you don't need any external jar files, I suggest you look here for basics on these and look here for examples.

Most of the places, I look for provides me the answer for GET. I see some POST too, but my requirement is mainly for PUT.

In order to place PUT, DELETE, etc.. http operations, you can use httpConnectionObj.setRequestMethod("PUT") or httpConnectionObj.setRequestMethod("DELETE") as given in an example here.

Community
  • 1
  • 1
developer
  • 19,553
  • 8
  • 40
  • 57
1

you could set up a simple java.net.Socket instance and implement the calls yourself - then you don't need any dependencies.

http://www.cafeaulait.org/course/week12/22.html

1

java.net.HttpURLConnection seems to be the way to go then. This might i.e. be initialized like so: (HttpURLConnection) new URL(url).openConnection()

ldz
  • 2,172
  • 14
  • 20