Home » Java » java – Rest service in Spring Boot that invokes a storage procedure and must return response in JSON-Exceptionshub

java – Rest service in Spring Boot that invokes a storage procedure and must return response in JSON-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Good afternoon, I am relatively new to spring boot, and I am working with a rest service that receives a telephone number as a parameter, and in turn in the database a stored procedure must be executed that should validate it, the same stored procedure will return A response that brings a code and response, both parameters must be presented in a json response to the request, attached example:

Input:

04258978417

Output:

{ "ValidateClient": { 
    "Body": {
         "Code": "NA22001", 
          "Message": "Fecha Maxima de asignacion del cliente.", 
             }
      }
}

enter image description here

The Storage Procedure is as follows:

CREATE PROCEDURE ValidacionClienteBonificado (p_movil varchar(11))
BEGIN

if exists (select '' from ClienteBonificados a where a.movil=p_movil) then
    if exists (select '' from ClienteBonificados a where a.contador=0 and a.movil=p_movil) then
        if (select a.fecha_fin<current_timestamp() from ClienteBonificados a where a.fecha_fin<current_timestamp() and a.movil=p_movil ) then
            select 'Fecha Maxima de asignacion del cliente' as result, 'NA22001' as code;
        else
            select 'Cliente aplica para bono' as result, '00000' as code;
        end if;
    else
        select 'Bono asignado a cliente' as result, 'NA22002' as code;
    end if;
else
    select 'Cliente no encontrado' as result, 'NA22003' as code;
end if;

At the moment I am creating the entity, but I have doubts how to place the output parameters, according to what I have investigated, since it does not return anything related to the parameters of the table:

table ClienteBonificados(
id int auto_increment primary key,
movil varchar(11) not null,
contador char(1) not null,
fecha_driver datetime,
fecha_alta datetime,
fecha_fin datetime,
codigo_transaccion varchar(5)
)

Then my code:

package com.app.validacion.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedStoredProcedureQueries;
import javax.persistence.NamedStoredProcedureQuery;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureParameter;
import javax.persistence.Table;

@Entity
@Table
@NamedStoredProcedureQueries({
    @NamedStoredProcedureQuery(
            name="ProcedueValidationCliente",
            procedureName="ValidacionClienteBonificado",
            parameters = {
                    @StoredProcedureParameter(mode=ParameterMode.IN, name="p_movil",type=String.class), 
                    @StoredProcedureParameter(mode=ParameterMode.OUT, name="result",type=String.class),
                    @StoredProcedureParameter(mode=ParameterMode.OUT, name="code",type=String.class),
            })

})
public class DriverBonificados {

    @Id
    private int id;
    private String movil;
    private String contador;
    private Date fecha_driver;
    private Date fecha_alta;
    private Date fecha_fin;
    private Date codigo_transaccion;

}

In this case the response of the database would go within the configuration of @NamedStoredProcedueQueries?

I have doubts on how to build my DAO / Repository, since I have done it, but with an answer related to the object or entity of the database

How to&Answers: