Post json file to my REST service in java -


i'm trying post data angular js form saved database using rest service using java. below code rest

@stateless @path("driverprops")  public class driverpropfacaderest extends abstractfacade<driverprop> {     @persistencecontext(unitname = "com.mycompany_groupprojectbackend_war_1.0-snapshotpu")     private entitymanager em;  public driverpropfacaderest() {     super(driverprop.class); }  @post @override @consumes({ "application/json"}) public void create(driverprop entity) {     super.create(entity); } 

hers part of driverprop class

import java.io.serializable; import javax.persistence.basic; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.namedqueries; import javax.persistence.namedquery; import javax.persistence.table; import javax.validation.constraints.notnull; import javax.xml.bind.annotation.xmlrootelement;     @entity     @table(name = "driverprop")     @xmlrootelement     @namedqueries({         @namedquery(name = "driverprop.findall", query = "select d   `driverprop d"),         @namedquery(name = "driverprop.findbydriverid", query = "select d driverprop d d.driverid = :driverid"),         @namedquery(name = "driverprop.findbygear", query = "select d driverprop d d.gear = :gear"),         @namedquery(name = "driverprop.findbychangeuprpm", query = "select d driverprop d d.changeuprpm = :changeuprpm"),         @namedquery(name = "driverprop.findbychangedownrpm", query = "select d driverprop d d.changedownrpm = :changedownrpm"),         @namedquery(name = "driverprop.findbypreferredmaxrpm", query = "select d driverprop d d.preferredmaxrpm = :preferredmaxrpm"),         @namedquery(name = "driverprop.findbypreferredminrpm", query = "select d driverprop d d.preferredminrpm = :preferredminrpm"),         @namedquery(name = "driverprop.findbydecisiontime", query = "select d driverprop d d.decisiontime = :decisiontime"),         @namedquery(name = "driverprop.findbymaxaccn", query = "select d driverprop d d.maxaccn = :maxaccn"),         @namedquery(name = "driverprop.findbymaxbreaking", query = "select d driverprop d d.maxbreaking = :maxbreaking"),         @namedquery(name = "driverprop.findbymaxstraightspeed", query = "select d driverprop d d.maxstraightspeed = :maxstraightspeed"),         @namedquery(name = "driverprop.findbymaxcornerspeed", query = "select d driverprop d d.maxcornerspeed = :maxcornerspeed")})     public class driverprop implements serializable {         private static final long serialversionuid = 1l;         @id         @generatedvalue(strategy = generationtype.identity)         @basic(optional = false)         @column(name = "driverid")         private integer driverid;         @basic(optional = false)         @notnull         @column(name = "gear")         private int gear;         @basic(optional = false)         @notnull         @column(name = "changeuprpm")         private float changeuprpm;         @basic(optional = false)         @notnull         @column(name = "changedownrpm")         private float changedownrpm;         @basic(optional = false)         @notnull         @column(name = "preferredmaxrpm")         private float preferredmaxrpm;         @basic(optional = false)         @notnull         @column(name = "preferredminrpm")         private float preferredminrpm;         @basic(optional = false)         @notnull         @column(name = "decisiontime") 

my question need angular js send json file , json should saved database using rest services.. idea achieve using angular fr client..

witrh angular, easiest way use $resource. in case, that:

var app = (...)  app.factory('driverpropfacaderest', function($resource) {     return $resource('http://(...)/driverprops/:id', {id:'@id'}); });  app.controller('driverpropcontroller', function($scope, driverpropfacaderest) {     var inputdata = { 'driverid': 'value', (...) };     driverpropfacaderest.save(inputdata, function(data) {         console.log(data);         (...)     }); }); 

see link in angular reference documentation more details: https://docs.angularjs.org/api/ngresource/service/$resource.

you use low level api $http, described below:

app.controller('driverpropcontroller', function($scope, $http) {     var inputdata = { 'driverid': 'value', (...) };     $http.post('http://(...)/driverprops', inputdata)          .success(function(data, status, headers, config) {               // callback called asynchronously               // when response available          })          .error(function(data, status, headers, config) {               // called asynchronously if error occurs               // or server returns response error status.          }); }); 

see link in angular reference documentation more details: https://docs.angularjs.org/api/ng/service/$http#post.

hope helps you, thierry


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -