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