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.

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 Commented Feb 16, 2020 at 15:07
  • you could give Jersey a try. eclipse-ee4j.github.io/jersey.github.io/documentation/latest/… Commented Feb 16, 2020 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. Commented Feb 16, 2020 at 15:09
  • @Shubh, I suppose I don't need servlets, it's a backend api and no front end is there. Commented Feb 16, 2020 at 15:09

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I know I won't gain any performance but that should save me a lot of time since there would be no effort in creating POJOs. But can you clarify more on how using POJOs is a better approach?
Creating POJO would make your code look very clean and readable.
Ok, Will take care thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.