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

C# Using tabs

To use tabs in C# use the TabContainer element from AjaxControlToolkit Include AjaxControlToolkit  Include in the Web.config file, inside the tag <system.web> the following code  <pages>       <controls>         <add tagPrefix="ajaxCTK" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>       </controls>     </pages>   Include TabContainer element First  include TabContainer element that is the section where all the tabs will be displayed. <ajaxCTK:TabContainer ID="TabContainerUpdate" runat="server"                 Height="800"                 CssClass="ajax__tab_style"> </ajaxCTK:TabContainer> Second per each tab include the following code corresponding to each ...

Rails - Basic Steps III

pValidations Validations are a type of ActiveRecord Validations are defined in our models Implement Validations Go to   root_app/app/models Open files  *.rb for each model Mandatory field validates_presence_of   :field Ex:   validates_presence_of    :title Classes The basic syntax is class MyClass        @global_variable                def my_method              @method_variable        end end Create an instance myInstance = MyClass.new Invoke a mehod mc.my_method class() method returns the type of the object In Ruby, last character of method define the behavior If ends with a question -> return a boolean value If ends with an exclamation -> change the state of the object Getter / Setter method def global_variable       return @global_variable end ...

Python create package

Create a root folder Create a sub-folder "example_pkg" that contains the funtionallity packaging_tutorial/ example_pkg/ __init__.py In the root folder create the following structure  packaging_tutorial/ example_pkg/ __init__.py tests/ setup.py LICENSE README.md in the setup.py contains the configuration of the packages your package is found by find_packages() import setuptools with open ( "README.md" , "r" ) as fh : long_description = fh . read () setuptools . setup ( name = "example-pkg-YOUR-USERNAME-HERE" , # Replace with your own username version = "0.0.1" , author = "Example Author" , author_email = "author@example.com" , description = "A small example package" , long_description = long_description , long_description_content_type = "text/markdown" , url = "https://github.com/pypa/sam...