0

My company has asked me to do a small project in Java Spring and I have never programmed in Spring nor Java before, so I am a bit lost.

I have been asked to make a series of endpoints, seeing the documentation does not seem difficult, where I find the difficulty is that I always receive as input a JSON with several key-value pairs, and with that, I have to search in an Oracle database using Hibernate and return another JSON with other key-value pairs.

Example:

Input:

{
  "client_id": 123,
  "shop_id": 22,
  "id_contract": 233
}

Output:

{ "loan": "70%"
  "percentage_point": "80%" 
}

My question is, should I do it through a POST method? Is the output a status 200? How do I get from JSON, for example, the client id to map it in the database with hibernate / Spring?

Any advice will be welcome, even if it is simply articles, etc.

Saurabh
  • 882
  • 1
  • 5
  • 16
  • What endpoints have they asked you to create? Have you tried anything yourself so far? – Jason Mar 06 '20 at 02:50
  • Something like client/newContract, normally would be just saving the new contract in the bbdd, but what they want is just return another JSON with information in the bbdd as I posted. I didn't start, because all endpoints are going to be like as I explain. So I should first figure out how to face it. –  Mar 06 '20 at 02:55
  • Yeah so for your GET you're going to simply return a ResponseEntity of type X that includes loan and percentage_point. – Jason Mar 06 '20 at 02:56
  • But using a GET method is not wrong receiving a JSON body as an input? –  Mar 06 '20 at 03:03
  • Well if the goal is to get all loans at a percentage point for a client at specific id, for example his id is #1 we can assume the endpoint is /client?id where id is 1. You would consume that id, use JPA to get the loan or collection of loans from the database and return them as a ResponseEntity. You're not modifying anything, it's a simple GET request. – Jason Mar 06 '20 at 03:06
  • Maybe I explained myself wrongly, I don't get only the id as input like client/id. What I get as an input is a JSON with different properties and in an endpoint like client/newContract. Example: input { "id_client": 1, "id_shop": 2} which is basically a body, not only a param with an id –  Mar 06 '20 at 03:16
  • What you should do is update the main post with **specific** details about the task. If you're creating a new contract for a client, potentially a new client, then yes that would be a POST since you're modifying. You would consume application/json as the MediaType and produce application/json as the MediaType. – Jason Mar 06 '20 at 03:18
  • It has to be my fault for explaining myself poorly because English is not my first tongue. I was trying to give you one example with the new contract endpoint to show, that I don't have to create a new contract in the bbdd, what they want is to retrieve information from the bbbd(in this case the loan and percentage in json format) with the json that we have as a input, you're not going to save or set anything in the bbdd, just retrieve information. Do you understand me? but with a body as a input, not an uuid or something like that in the params of the uri. –  Mar 06 '20 at 03:35
  • Okay, no worries. I'll see what I can do. – Jason Mar 06 '20 at 03:40
  • Thanks Jason, you are being of great help. –  Mar 06 '20 at 03:42
  • No worries. The one thing I keep coming back to is that it's odd that they want to send JSON and get JSON back but only retrieve information. It sounds like a GET but you would never send JSON in the request body. On the other hand you would never send a POST that consumes JSON but only retrieves data. It's a very weird thing they're asking you to do, or how you have explained it. – Jason Mar 06 '20 at 03:52
  • If you could post the original requirements they gave you that would be amazing. – Jason Mar 06 '20 at 03:53
  • They want for example 3 endpoints, one for creating a new contract, one for renewal one contract and another for repurchasing a contract. in the 3, I receive as input a JSON with the customer code, another from the store, another from the contract, for example, and in all those endpoints I always have to take something from the bbdd and simply return it, as if it were a GET methods, but I am receiving a JSON with several parameters at once as an input. I don't know how to do that. but it's how they are asking me. They don't specify me the methods that I have to use just this. –  Mar 06 '20 at 04:06
  • Input: JSON file with the following information Customer Id Id Store Id Contract Output: JSON file with the following information % interest percentage Loan –  Mar 06 '20 at 04:08
  • So that is vastly different from retrieving information from a database. This is about creating a new contact, renewing one, and repurchasing. This would use POST. – Jason Mar 06 '20 at 04:09
  • They want to name the endpoints like that, but I don't have to update or create information to the bbdd. I know is weird af, but when I call those endpoints, I have to retrieve information from the bbdd with the input JSON in mind. Do you understand now? –  Mar 06 '20 at 04:15
  • Is this for an interview or for a job? It sounds to me like the concept of HTTP methods are being abused and will lead to problems down the road. If it for an interview, the interviewer has no idea what they're doing. If it's neither of those, it's your understanding of the problem. – Jason Mar 06 '20 at 04:17
  • It's for a job. If it were normal crud, it would be easy, but trust me the requirements are defined as I wrote. But it's weird too, that anybody faces a problem like this one. I think is a post with a result of a get so to speak. –  Mar 06 '20 at 04:24
  • Talk to your project manager or other developers o your team. Ask them why they want to do it this way. – Jason Mar 06 '20 at 04:37

2 Answers2

1

Well what you have been asking is not for a specific problem.It is an entire requirement. I can try helping with the steps that you may need to follow.

If you are not done creating a project, I would suggest you to use Spring Boot for your project.To create a spring boot app click here

And if you are using maven or gradle select it and then search for Spring Web,Spring Data JPA in search dependencies and generate a project.

Once you have a project ready and imported it into an IDE (for example Eclipse, IntelliJ Idea, Netbeans or any other Java IDE), and finally all you have to do is to write REST services to implement your requirements.

And to answer your questions.

should I do it through a POST method?

If you were told that the request you will be receiving is in a JSON format, then you need to use POST method.

What is post and why to use post method.check here

Is the output a status 200?

A status 200 in http means SUCCESS. It means when the data sent by the client is valid and the server has processed the request(in your case JSON body) successfully it returns 200.So incase of success your api will return 200.

More on http status codes

How do I get from JSON, for example, the client id to map it in the database with hibernate / Spring?

To understand more on how to create a rest controller, take JSON input, create a POST method, use Spring JPA for making database operations please follow the links below:

create a rest service

spring boot crud Database operations example from scratch

Spring boot Data JPA example

Hope this will help you to put you in right path.

Brooklyn99
  • 987
  • 13
  • 24
  • Precisely. He was saying English wasn't his first language so that is understandable but it's just a poor explanation of the requirement needed. – Jason Mar 06 '20 at 04:12
0

Without code or a more specific post about requirements it's impossible to help. All I can do is provide links to potentially helpful articles. You need to look into the following;

Spring Boot RESTful service

Spring Boot Controller vs RestController

Spring Boot JPA

Spring Boot Consuming/Producing JSON

Jason
  • 5,154
  • 2
  • 12
  • 22