Save Request Spring Boot

Asked

Viewed 864 times

2

I’m a beginner at Spring Boot and I need a little help. I want to save an incoming and outgoing request by generating the automated id and saving the date and time, using Docker and Postgres (I don’t know if anything changes to say I’m using Maven either. Forgive my lack of knowledge, I’m learning kkkk). With this code, the application started, but it didn’t work for what I want. Can you please help me?

I know you may have some things that don’t make sense, but it’s because of the attempts I’ve been making to try to help you over the Internet. Thank you so much! I want to begin to understand better!

ID - Automatically generate and save to database DATE AND TIME - save current date and time AND IF IT’S AN INCOMING OR OUTGOING REQUEST: json

Filename I wanted to apply pathvariable, since I can have several filenames, but in this situation, I also don’t know how to implement.

Meh 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);
    }


}

Requisitioning

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;
    }
}

Requisicaorepository

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

1 answer

0


I believe that the insertion of logs for request records is the most adopted model and suitable for this situation, however, if you still choose to persist all requests of your application, you can create a request processor. So all requests will pass through the interceptor class and you can save the information in your database.

  1. For insertion of logs

2.1. To create the Third Party, you first create a class to register your Third Party(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 creates 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);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.