Search code examples
javajsonspringspring-mvcjackson-databind

Spring MVC how to convert Java Object to jsonarray from Oracle DB


Spring Version: 3.9.15.RELEASE OpenJDK8

I want to convert Java Object that is derived from Oracle DB into JSON array.
I need to put JSON array's data into a data table which allows only JSON array type as an input.
I have been trying to use jackson-databind but I'm still struggling.
I won't need any parameter to call the data because the page I'm working doesn't take any further input to refresh or update the data table.

    [ {
            "ID" : "sample1",
            "nickname" : "올드희",
            "name": "김영희",
            "phone": "010-1234-5678",
            "email": "sample1@gmail.com"
        },
        {
            "ID" : "sample2",
            "nickname" : "짱구친구",
            "name": "김철수",
            "phone": "010-5555-1111",
            "email": "sample2@gmail.com"
        } ]

Above is the desirable Jsonarray form I want to get.
Below is the select query of mapper file.

<select id="selectNormalList" parameterType="string" resultType="Member">
    SELECT
           M.ID
         , NICKNAME
         , NAME
         , PHONE
      FROM
           MEMBER M
 LEFT JOIN
           M_NORMAL N ON(M.ID = N.ID)
     WHERE
           GRADE = 'N'
</select>

If I have to use jackson-databind because there is no alternative,
Q1) Is Ajax necessary to call the data?
Q2) Is @PathVariable necessary?

If not, I'll be very appreciate with your advice.


Solution

  • You can use spring REST api to write data in json format and save it into java object and insert into database or also you can select data from databases and take it into java object and display data as json format.

    To write data in json and save into database you can use postman api and also you need to change in your controller @notations at method :

    1. To save data from json to database set notation @PostMapping.
    2. To retrieve data from the database and show in json format set notation @GetMapping.
    3. TO update data you can set @PutMapping as a method.
    4. To delete data from database set @DeleteMapping. and a lot more operations you can do at postman rest api.

    references :

    1. postman api for ubuntu 2.follow this image at postman
    • select method that you want operation to do.
    • insert your local host url at url box.
    • select body and raw then insert your data in json format.
    • then hit on send button.
    • and check at data base or select get method and send and check data is added or not.