Ir al contenido principal

Spring Boot - Web Apps

Web Apps


  • Create a folder resources for the view in
<PROJECT>\src\main
  • Create a subfolder names public. In this folder put the JS files, html files, images, etc
<PROJECT>\
          ->  src\
                  ->main
                             ->java
                             ->resources
                                      ->public
                                            ->index.html


  • Ex. Add a file named index.html
To access the file you start the project and then access though the explorer as:
http://localhost:8080/index.html#/



SPRING MVC REST Controller


  • GET   /api/v1/<elements>  (list)
  • POST /api/v1/<elements>  (add)
  • GET /api/v1/<elements>/{id}  (view)
  • PUT /api/v1/<elements>/{id}  (update)
  • DELETE /api/v1/<elements>/{id}  (delete)

Creating a Controller

  • Create them in controller folder

    • Name the file as 
      • <Entity>Controller.java

    • Add the annotation to create the controller

      • @RestController

    • Add a second annotaion to define the URL

      • @RequestMapping("<BASE_url>/")    
      • Ex  @RequestMapping("api/v1")

  • Add a list endpoint (methods)

    • Add the method with the annotion @RequestMapping

@RestController
@RequestMapping("api/v1")
public class EvaluationController {


@Autowired
private EvaluationRepository evaluationRepository;

@RequestMapping(value = "evaluation", method = RequestMethod.GET)
public List<Shipwreck> list() {

return shipwreckRepository.findAll();
}


        @RequestMapping(value = "shipwrecks", method = RequestMethod.POST)
public Shipwreck create(@RequestBody Shipwreck shipwreck) {
return shipwreckRepository.saveAndFlush(shipwreck);
}

@RequestMapping(value = "shipwrecks/{id}", method = RequestMethod.GET)
public Shipwreck get(@PathVariable Long id) {
return shipwreckRepository.findOne(id);
}

@RequestMapping(value = "shipwrecks/{id}", method = RequestMethod.PUT)
public Shipwreck update(@PathVariable Long id, @RequestBody Shipwreck shipwreck) {
Shipwreck existingShipwreck = shipwreckRepository.findOne(id);
BeanUtils.copyProperties(shipwreck, existingShipwreck);
return shipwreckRepository.saveAndFlush(existingShipwreck);

}


}
  • Create the entities to return the Result in the folder MODEL
    •  scr/main/java/<project>/model


Invoke from AngularJS



  • Create a service

    • This contains the URL to your endpoint
    • include $resource


(function() {

var app = angular.module('app');

app.factory("evaluationService", function($resource) {

return $resource('/api/v1/evaluation/:id', {
id : '@id'
}, {
update : {
method : 'PUT'
}
});

});

}());


  • Invoke from the controller

    • Pass the service previously created
    • evaluationService.query();   
      • Get all the results
      • query() is a function from $resource

(function() {

var experienceEvaluationController = function($scope, $location, $anchorScroll, evaluationService) {
$scope.evaluationService = evaluationService.query();
}

var app = angular.module('app');

app.controller("experienceEvaluationController", experienceEvaluationController);

}());





Mapping with $resource


  • GET   /api/v1/<elements>  (list)
    • evaluationService.query();

  • POST /api/v1/<elements>  (add)
 $scope.evaluationService  = new evaluationService ();  //create new evaluationService instance. Properties will be set via ng-model on UI

  $scope.addEvaluation = function() { 
           evaluationService .$save(function() {

    });
  };

  • GET /api/v1/<elements>/{id}  (view)

evaluationService.evaluationService


  • PUT /api/v1/<elements>/{id}  (update)

 $scope.updateShipwreck = function() { 
    evaluationService.$update(function() {

    });
  };

  • DELETE /api/v1/<elements>/{id}  (delete)
 evaluationService.$delete(function() {
        $state.go('evaluation');
      });



Configuring App


All the properties are in Spring Properties

  • Create a properties file in the folder resources
    • application.properties - This is the default properties file to be used by Spring Boot

  • Creating  a log
    • Now you can create a log directly
    • logging.level.org.springframework.web = DEBUG

  • Server Port
    • server.port=8180

  • Productive / Test configuration
Name the files as follows:
    • application-prod.properties
    • application-test.properties
Then in the Run Configuration, pass as VM argument the profile
    • -Dspring.profiles.active=prod

































Comentarios

Entradas populares de este blog

Android - Basic Steps (Service)

Service Run in the main thread of the hosting application Android can kill the Service if need the resources Purpose Supporting inter-application method execution Performing background processing Start a Service Call Context.startService(Intent intent)\ To call from a Fragment use getActivity().getApplicationContext().startService( intentService); Executing the service After call startService(...)  In the Service is executed the method onStartCommand(...) If the method returns the constant START_NOT_STICKY then Android will not restart the service automatically if the the process is killedp Foreground To execute the service foreground call the method startForeground() Use this if the user is aware of the process Bind to a Service Call the method Context.bindService( Intent service ServiceConnection con int flags ) Send Toast from the Service On the method onStartCommand receive the message   ...

Android - Basic Steps (Location & Maps)

Location Is composed by Latitude Longitude Time-stamp Accuracy Altitude Speed Bearing LocationProvider Types: Network  Wifi access points Cell phone towers GPS Passive Piggyback on the readings requested by other application Permissions Network  android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION GPS android.permission.ACCESS_FINE_LOCATION Passive Provider android.permission.ACCESS_FINE_LOCATION LocationManager System service for accessing location data getSystemService( Context.LOCATION_SERVICE ) Functions Determine the last known user location Register for location update Register to receive intents when the device nears or move away from a given geographic area LocationListener Defines callbacks methods that are called when Location or LocationProvider status change. Methods onLocationChanged(...) onProviderDisabled(...) onProviderEnabled(...) onStatusChan...

IIS - Permisions

IIS Permissions To enable the Active Directory connection in the IIS, follow the next steps: Go to IIS Go to Application Pool Select your App Pool Select Advanced Settings in the right side In the section Process Model Select in Identity value the property " NetworkService " You don´t need to restart your application