Save Spring Boot request

2

I'm a beginner in Spring Boot and I need a little help. I want to save an incoming and outgoing request, generating the automatic id and saving the date and time, using docker and Postgres (I do not know if it changes either to say that I'm using maven.) Forgive even my lack of knowledge, I'm learning kkkk ). With this code, the application startedou, but it did not work for what I want. Can you please help me?

I know there may be some things that are not logical, but it's because of the attempts I made trying to get help online. Thanks in advance! I want to start understanding better!

ID - Automatically generate and save to database DATE AND TIME - save the current date and time AND IF IT IS AN INPUT OR OUTPUT REQUIREMENT: json

Filename I wanted to apply pathvariable, since I can have multiple file names, but in this situation, I also do not know how to implement.

Mind code:

Application

package requisc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }

}

ApplicationController

package requisc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
public class ApplicationController {


@Autowired
RequisicaoRepository requisicaoRepository;

@RequestMapping (path="/process" + "/{filename}/" + "entry")
public Requisicao getEntry(@RequestBody @Valid Requisicao requisicao) {
    return requisicaoRepository.save(requisicao);
    }
@RequestMapping (path="/process" + "/{filename}/" + "exit")
public Requisicao getExit(@RequestBody @Valid Requisicao requisicao) {
    return requisicaoRepository.save(requisicao);
    }


}

Requisition

package requisc;

import javax.persistence.*;
import java.util.Calendar;


@Entity
public class Requisicao{



@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@Column(name="data_requisicao")
@Temporal(TemporalType.TIMESTAMP)
private Calendar dataRequisicao;

@Column(name="requisicao")
private String requisicao;

public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getRequisicao() {
    return requisicao;
}
public void setRequisicao(Requisicao <S, O> requisicao) {
    this.requisicao = String.valueOf(requisicao);
}

public Calendar getdataRequisicao() {
    return dataRequisicao;
}

public void setdataRequisicao(Calendar dataRequisicao) {
    this.dataRequisicao = dataRequisicao;
    }
}

RequisitionRepository

package requisc;

import org.springframework.data.jpa.repository.JpaRepository;


public interface RequisicaoRepository extends JpaRepository<Requisicao,         String> {

}

application-local.yml

db.name: integracaodb


spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/integracaodb
    username: postgres
    password: postgres
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQL94Dialect
        hbm2ddl:
          auto: update
        temp:
          use_jdbc_metadata_defaults: false
    database-platform: org.hibernate.dialect.PostgreSQL9Dialect
    
asked by anonymous 14.07.2018 / 18:07

1 answer

0

I believe that inserting Logs into request records is the most appropriate and appropriate template for this situation, however, if you still choose to persist all requests in your application, you can create a request interceptor. So all requests will pass through the interceptor class and you can save the information in your database.

  • To insert logs
  • 2.1. To create the interceptor, you first create a class to register your interceptor (s).

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    public class InterceptorRegister extends WebMvcConfigurerAdapter {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new Interceptor());
        }
    }
    

    2.2. Then create the interceptor class

    import java.util.Calendar;
    import java.util.UUID;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    public class Interceptor extends HandlerInterceptorAdapter {
    
        @Autowired
        RequisicaoRepository requisicaoRepository;
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            Requisicao requisicao = new Requisicao();
            requisicao.setRequisicao(UUID.randomUUID().toString());
            requisicao.setDataRequisicao(Calendar.getInstance());
            requisicaoRepository.save(requisicao);
            return super.preHandle(request, response, handler);
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            Requisicao requisicao = new Requisicao();
            requisicao.setRequisicao(UUID.randomUUID().toString());
            requisicao.setDataRequisicao(Calendar.getInstance());
            requisicaoRepository.save(requisicao);
            super.postHandle(request, response, handler, modelAndView);
        }
    }
    
        
    28.08.2018 / 20:49