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