Web Apps
- Create a folder resources for the view in
- Create a subfolder names public. In this folder put the JS files, html files, images, etc
-> src\
->main
->java
->resources
->public
->index.html
- Ex. Add a file named index.html
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)
$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
- application-prod.properties
- application-test.properties
Then in the Run Configuration, pass as VM argument the profile
- -Dspring.profiles.active=prod
Comentarios
Publicar un comentario