Tutoriales gratuitos para el aprendizaje de la programacion informatica! Recuerda que si lo puedes imaginar... lo puedes programar!

Crear servicios en angular

 Para poder conectar un servicio creado en Java Spring y que sea visualizado en Angula se debe hacer lo siguiente:

En angular

Antes de crear los servicios se debe declarar una const para direccionar tu proyecto

En environments >> environment.ts crear una const con la url de base de tu proyecto:

export const environment = {
    production: false,
    NOMBRE-URL:'escribe aquí la direccion base/ '
};

Luego crear los servicios:

Comando:

    ng g s NOMBRE-SERVICIO

Dentro de app, se crearan dos archivos .ts :

  • TS NOMBRE-SERVICIO.service.spec.ts
  • TS NOMBRE-SERVICIO.service.ts
Entra en NOMBRE-SERVICIO.service.ts  dentro de la class  NOMBRESERVICIOService, escribe los métodos tu servicio:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';

@Injectable({
providedIn: 'root'
})
export class  NOMBRESERVICIOService {

protected resourceEndPoint = environment.NOMBRE-URL

constructor(private httpClient : HttpClient) { }

// Ver una lista:

getListaNOMBRE-OBJETO(){
    return this.httpClient.get(`${this.resourceEndPoint}NOMBRE-OBJETO/list`);
}

/*
Todo depende de como fue creado los servicios en BE:

>> Ejemplo de como fue creado en el BE:

@RequestMapping(path = "/NOMBRE-OBJETO")
public interface AliquotaIvaAPI {

@GetMapping(path = "/list" ,produces = "application/json")
public ResponseEntity<ArrayList<NOMBREOBJETOResponse>> getAll(HttpServletRequest request);

}

*/

//Ver un objeto

getNOMBRE-OBJETO(id : number){
return this.httpClient.get(`${this.resurcesEndPoint}NOMBRE-OBJETO/findById/${id}`);
}

/*
>> Ejemplo de como fue creado en el BE:


@GetMapping(path = "/findById/{id}", produces = "application/json")
public ResponseEntity<
NOMBREOBJETOResponse> getById(HttpServletRequest request, @PathVariable Integer id);

*/

//Modificar

updateNOMBRE-OBJETO(id: number,object : any){
return this.httpClient.post(`${this.resurcesEndPoint}NOMBRE-OBJETO/insertUpdate?id=/${id}`,object);
}

/*
>> Ejemplo de como fue creado en el BE:

@PostMapping(path = "/insertUpdate", produces = "application/json", consumes = "application/json")
public ResponseEntity<NOMBRE-OBJETOResponse>
insertUpdate(HttpServletRequest request, @RequestBodyNOMBRE-OBJETORequest oNOMBRE-OBJETORequest);

*/

// Nuevo

creaateNOMBRE-OBJETO(object : any){
return this.httpClient.post(`${this.resurcesEndPoint}NOMBRE-OBJETO/insertUpdate?id=/`,object);
}}

/*
>> Ejemplo de como fue creado en el BE: en este ejemplo fue creado un solo servicio para gregar nuevo y para modificar

@PostMapping(path = "/insertUpdate", produces = "application/json", consumes = "application/json")
public ResponseEntity<NOMBRE-OBJETOResponse>
insertUpdate(HttpServletRequest request, @RequestBodyNOMBRE-OBJETORequest oNOMBRE-OBJETORequest);

*/

//Eliminar

delete(id: number){
return this.httpClient.delete(`${this.resurcesEndPoint}NOMBRE-OBJETO/deleteById/${id}`);
}

/*
>> Ejemplo de como fue creado en el BE:

@DeleteMapping(path = "/deleteById/{id}", produces = "application/json")
public ResponseEntity<Integer> deleteById(HttpServletRequest request, @PathVariable Integer id);

*/

}

En eclipse ir a la interface API >> com.example.demo.data.controller.api en NOMBRE-OBJETOAPI.java  declarar: @CrossOrigin

@CrossOrigin
@RequestMapping(path = "/NOMBRE_RUTA")
public interface NOMBRE-OBJETOAPI{

}

No hay comentarios:

Publicar un comentario