2

Hi I am new in spring And developing a module of POST. How to insert JSON array in db. can you please give me the idea how to solve this issue.

I have also an example to show you this. link:- http://hello-angularjs.appspot.com/angularjs-http-service-ajax-post-json-data-code-example

Here the controller code

@RequestMapping(value = "/create1", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
BillingModel addbillingmodel(@RequestBody BillingModel billingmodel) {
    try { 


        billingmodel.getItemid();
        billingmodel.getQuantity();

        dataServices.addEntity(billingmodel); 


        return billingmodel;
    } catch (Exception e) {
         e.printStackTrace();
        return billingmodel;
    }

   }
  }

Here is my html page with JSON.

<!DOCTYPE html>
<html data-ng-app="serviceModule">
<head>
<meta charset="ISO-8859-1">
<title>AngularJS POST Spring MVC</title>
<script    
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js">   
</script>
<script type="text/javascript">

var serviceModule = angular.module('serviceModule', []);

serviceModule.controller("AngularJSPostController", function($scope, $http) {

        $scope.variable = "AngularJS POST Spring MVC Example:"; 
        var dataObj = {
                "itemid" : "11",
                "quantity" : "22",


        };      

        var response = 
   $http.post('/CRUDWebAppMavenized/billing_bakery/create1', dataObj);
        response.success(function(data, status, headers, config) {
            $scope.responseData = data;
        });
        response.error(function(data, status, headers, config) {
            alert( "Exception details: " + JSON.stringify({data: data}));
        });

    });

 </script>
 </head>
 <body data-ng-controller="AngularJSPostController"> 




  <div>
    <h4>{{variable}}</h4>
    <b>You had sent below data through post:</b>
    <p>Response:  {{responseData}}</p>      
  </div>

  </body>
</html>

I have to multiple data in a row.

Tintumon M
  • 1,156
  • 2
  • 14
  • 36
Mehandi Hassan
  • 2,381
  • 4
  • 16
  • 20

1 Answers1

0

1.In your BillingModel class create following methods -

     import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
     import com.fasterxml.jackson.core.JsonParseException;
     import com.fasterxml.jackson.core.JsonProcessingException;
     import com.fasterxml.jackson.databind.JsonMappingException;
     import com.fasterxml.jackson.databind.ObjectMapper;
     public String toJson() throws JsonProcessingException {
       ObjectMapper mapper = new ObjectMapper();
       String json = mapper.writeValueAsString(this);
       return json;
    }

2.You should have blob types of columns to store JSON

3.And in Your DAO class when you call addEntity , do something like this -

final MapSqlParameterSource sqlParams = new MapSqlParameterSource()
.addValue("item_json",billingModel.toJson().getBytes("UTF-8"));

You can also refer. Converting Java objects to JSON with Jackson

Community
  • 1
  • 1
csarathe
  • 420
  • 1
  • 5
  • 12
  • 2.You should have blob types of columns to store JSON ??? Actually i want to insert JSON data as string values in table columns so why should i use blob? – Mehandi Hassan Aug 18 '15 at 05:37
  • String json is just a String , if you want to store in VARCHAR you can also do that.in mysql there is text type of column to store long string data – csarathe Aug 18 '15 at 06:11