Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Created May 18, 2024 21:45
Show Gist options
  • Save mayankchoubey/8a12503a13f8571af1e40838c9d41b5e to your computer and use it in GitHub Desktop.
Save mayankchoubey/8a12503a13f8571af1e40838c9d41b5e to your computer and use it in GitHub Desktop.
Spring Boot - PG read app
server.port=3000
spring.threads.virtual.enabled=true
spring.datasource.url= jdbc:postgresql://localhost:5432/${dbName}
spring.datasource.username= ${dbUser}
spring.datasource.password= ${dbUserPass}
spring.datasource.max-active=10
spring.datasource.max-idle=10
spring.datasource.min-idle=10
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
package com.example.demo;
public class NoUserResponse {
}
package com.example.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
@Table(name = "users")
public class User {
@Id
private String email;
private String first;
private String last;
private String city;
private String county;
private int age;
public String getId() {
return email;
}
public void setId(String email) {
this.email = email;
}
public String getFirst() {
return first;
}
public void setFirst(String name) {
this.first = name;
}
public String getLast() {
return last;
}
public void setLast(String name) {
this.last = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
package com.example.demo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
import com.example.demo.UserRepository;
import com.example.demo.User;
import com.example.demo.UserRequest;
import com.example.demo.NoUserResponse;
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@PostMapping("/")
public ResponseEntity handleRequest(@RequestBody UserRequest userRequest) {
Optional<User> user = userRepository.findById(userRequest.userEmail);
if(user.isPresent()) {
return new ResponseEntity(user.get(), HttpStatus.OK);
}
return new ResponseEntity(new NoUserResponse(), HttpStatus.NO_CONTENT);
}
}
package com.example.demo;
import org.springframework.data.repository.CrudRepository;
import com.example.demo.User;
public interface UserRepository extends CrudRepository<User, String> {
}
package com.example.demo;
public class UserRequest {
public String userEmail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment