-1

I'm new to programming and I got a project to make a rest API. I made one using spring boot to use GET and POST operations. Now I'm trying to get it to work on a simple java application where,

  • The user inserts their first name using the java scanner.
  • Then sends a POST request to send it to a database.

So far the API works fine in Postman but I have no idea on how to add it to java using a method.

( I used this tutorial to make the api : https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/ )

Hisham Bawa
  • 438
  • 5
  • 14
  • https://www.baeldung.com/rest-template – Simon Martinelli Mar 30 '20 at 08:41
  • don't understand what you really want to do, you should be able to do this with spring boot and jpa. – Aymen Ragoubi Mar 30 '20 at 08:42
  • @SimonMartinelli, I went through most of the tutorials in baeldung, but I couldnt understand anything. I even tried downloading his source code so i can see how it works but even that gave out an error – Hisham Bawa Mar 30 '20 at 08:44
  • @AymenRagoubi im trying to make a simple app thats similar to a hello java without an interface or anything. It just asks the user to enter his first name. After you type something and hit enter i want to call a POST request so I can send it to the mySql database. Basically I want to do the thing i do on postman in the IDE itself – Hisham Bawa Mar 30 '20 at 08:46
  • If you're new to programming maybe don't start with a stack that requires years of training and learning. Create a Java command line application first and learn the problems and solutions for this before starting to jump through the next big hoops that require you to not only understand programming but also HTTP communication and more. Tutorials might be a nice way to get things starting but they mostly don't help you understanding things left and right of their very reduced scenario. – Smutje Mar 30 '20 at 08:59
  • Why don't you try sending the data from the post request by sending the json data to the controller ? If u take the data from the Scanner you can simply call your service method to save the data in the repository. – Fatema Khuzaima Sagar Mar 30 '20 at 09:05
  • @Smutje I agree with you, but this is actually for a project I was assigned at work (Took a job as a trainee because there was a 6 month gap between the next intake for a degree) Most of these work make me super confused but i was asked to research and do this :( – Hisham Bawa Mar 30 '20 at 09:15
  • @FatemaSagar Im not sure how to do that – Hisham Bawa Mar 30 '20 at 09:15

4 Answers4

1

Good you are learning.. keep it up.. Soultion for your question is

You have to implement one RestClient using RestTemplate of Spring or other ( How do you create a REST client for Java? )

Say you have your endpoint http://localhost:8081/api/name

enter image description here

You have to create RestClient like this ( I have used simple one just for example purpose )

enter image description here

Sri
  • 437
  • 1
  • 4
  • 13
0

You should implement a java rest client that call you back end spring boot api that interactes with your database. To do it you can use a lot of java rest client api. See here rest client api

Aymen Ragoubi
  • 298
  • 5
  • 22
  • Ill check that now. But isnt there a way i can use the spring boot api i already made? The only way i know to insert data is using postman but there must be a way to use it within java? – Hisham Bawa Mar 30 '20 at 09:17
  • You spring boot api is just endpoints that you could call. for example if you have and endpoint like this /searchdata/{id} you have to connect to your sping api from your java program and then call the endpoint by passing parameters (here it's a path parameter 'id). As I said in my answer there is a couple of java api allowing you to do this. – Aymen Ragoubi Mar 30 '20 at 09:23
  • if you find this useful you have to accept my answer – Aymen Ragoubi Mar 30 '20 at 09:35
0

This is how to POST using a spring application as a REST client, assuming you're POSTing JSON, i.e. the "{ ... }" bit and receving JSON in the return:

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<String> payload = new HttpEntity<>("{ ... }", headers);

RestTemplate restTemplate = new RestTemplate();
String jsonFromServer = restTemplate.postForObject(url, payload, String.class);
codebrane
  • 4,290
  • 2
  • 18
  • 27
  • yeah, I have to enter info and I receive them as a JSON. Where exactly should I use that code? In the application.java or as a separate file? – Hisham Bawa Mar 30 '20 at 10:14
0

What you can do is:

  1. You will require this json to send data:

{
"firstname" : "xyz"
}

The code is simple just go through this tutorial: http://zetcode.com/java/getpostrequest/

Follow the same steps and you can achieve your goal. Try to replace the Model with your required data.

  • i already made a very simillar api and was wondering if i can use a java class to use post request etc instead of using postman – Hisham Bawa Mar 30 '20 at 10:58
  • Can u please elaborate ur question, as it is not quite clear of what you are trying to do. – Fatema Khuzaima Sagar Mar 30 '20 at 11:32
  • So i made a spring boot api to GET and POST a users first name and lastname. In order to use it, i have to use Postman and it works fine. I was hoping to find a way to make a java method where i can ask for user input (Using java scanner) and directly send the info to the database, without having to use postman to add data. – Hisham Bawa Mar 30 '20 at 13:01
  • Try the above link, that is the simplest code to satisfy your goal. – Fatema Khuzaima Sagar Mar 30 '20 at 13:46