Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Help with Java REST API

 
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.

So I need to make a POST call with Java to a REST Service. My code works fine, I am just writing in order to ask if there are better ways to do this, or if someone could refactor my code and make it more beautiful or with better performance.
I decided to user the Gson library, provided by Google.

I am giving an Employee Id and an email, phone, etc.
The input of the POST method should be in this format.





and the response I get is in this format:





So I wrote the following code, but I am uncomfortable with the input Json string... I mean what if i only want to send only one attribute (as following)






Then I have to do major adjustments in the code....

My code is the following. Any comments are welcome ! This is my first time to interact with REST/Json elements.








 
Saloon Keeper
Posts: 6650
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So I wrote the following code, but I am uncomfortable with the input Json string... I mean what if i only want to send only one attribute (as following)


Why on earth are you hardcoding JSON instead of using the API to create it? That would make the code much more maintainable.
 
Ioanna Katsanou
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I will create a seperate Employee class.

I will post code in a few minutes.

thanks
 
Ioanna Katsanou
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created the following class:







Is this the right way?
 
Tim Moores
Saloon Keeper
Posts: 6650
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not really what I meant. The JSON is still constructed "manually" rather than programmatically. Also,there is no point in adding formatting to it, as the receiving end will ignore that anyway.
 
Ioanna Katsanou
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain what you mean?

I am new at this..

thanks
 
Ioanna Katsanou
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I researched a little. I understood what you mean. I will post new code in a little.
 
Ioanna Katsanou
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So.

I created the Employee Class:






I remove the toString method from the Employee class.


and then I wrote the following code:


 



 
Ioanna Katsanou
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I add a toString method in Employee class?
This will be hardcoded Json....
 
Tim Moores
Saloon Keeper
Posts: 6650
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


          strBu.insert(0, "{\n" +
          "\"EMPLOYEES\": {\n" +
          "\"LIST\": [");
           
          strBu.append("]\n" +
          "}\n" +
          "}");


This is still hardcoded JSON. See https://www.tutorialspoint.com/json/json_java_example.htm or https://www.baeldung.com/java-org-json for examples of how to create JSON via an API.
 
reply
    Bookmark Topic Watch Topic
  • New Topic