0

I have a nodejs server application that provides API. Due to some client issues, we need to implement the same server using java. In node server, for GET or POST we simply execute SQL, GET or POST data to database using JSON. But in Java, I have seen using spring boot, I have to create a Bean class and in my controller return a List in @GetMapping.

For all my APIs, I would have to create so many beans. Can I return data just like in nodejs, executing SQL query, getting and returning JSON data as it is in Java? Also, if possible, is this approach recommended or the creating Bean approach is better? I am new to java so, have less idea about this.

Player_Neo
  • 1,413
  • 2
  • 19
  • 28
Rahul Midha
  • 31
  • 1
  • 4
  • Yes you can use traditional jdbc and servlets ,if you can write efficient code which does your job and doesn't consumes your resources much,otherwise go with frameworks like spring or spring boot – Shubham Dixit Feb 16 '20 at 15:07
  • you could give Jersey a try. https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/index.html – Alice Oualouest Feb 16 '20 at 15:07
  • @BhanuHoysala, But in case of java, I don't want to create so many bean classes. I just want to fetch data and send in response. – Rahul Midha Feb 16 '20 at 15:09
  • @Shubh, I suppose I don't need servlets, it's a backend api and no front end is there. – Rahul Midha Feb 16 '20 at 15:09

1 Answers1

0

I see nothing wrong in creating beans to transfer data from the database to consumer. I don't see any kind of performance or efficiency you would achieve by directly transferring data from SQL database to consumer using Spring MVC to send over HTTP, It even adds more complexity in Java specifically at persistence layer. You should have been aware of that Java is verbose language before switching from NodeJS. It is better to create an equivalent POJOs.

If you still wanted to go with your own way of doing, following any of these existing posts you will achieve your expectation.

How to read/retrieve data from Database to JSON using JDBC?

Most efficient conversion of ResultSet to JSON?

Json object from database in java

Player_Neo
  • 1,413
  • 2
  • 19
  • 28