Код: Выделить всё
pom.xml
Код: Выделить всё
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-validation
org.springframework.boot
spring-boot-devtools
runtime
true
com.mysql
mysql-connector-j
runtime
org.postgresql
postgresql
runtime
org.springframework.boot
spring-boot-starter-test
test
Код: Выделить всё
TypeHunterModel.java
Код: Выделить всё
package com.user.springboot3.project.Model;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
@Entity
@Table(name="type_hunters")
public class TypeHunterModel {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@NotBlank(message = "Description is required")
@Column(name="description")
private String description;
public TypeHunterModel() { // No-arg constructor for JPA
}
public TypeHunterModel(int id, String description) {
this.id = id;
this.description = description;
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
}
Код: Выделить всё
TypeHunterDAO.java
Код: Выделить всё
package com.user.springboot3.project.DAO;
import com.user.springboot3.project.Model.TypeHunterModel;
import java.util.List;
public interface TypeHunterDAO {
TypeHunterModel create(TypeHunterModel theTypeHunterModel);
TypeHunterModel update(TypeHunterModel theTypeHunterModel);
}
Код: Выделить всё
TypeHunterServiceImplementation.java
Код: Выделить всё
package com.user.springboot3.project.Service;
import com.user.springboot3.project.DAO.TypeHunterDAO;
import com.user.springboot3.project.Model.TypeHunterModel;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TypeHunterServiceImplementation implements TypeHunterDAO {
@PersistenceContext
private EntityManager entityManager;
public TypeHunterServiceImplementation(EntityManager theEntityManager) {
this.entityManager = theEntityManager;
}
@Override
@Transactional
public TypeHunterModel create(TypeHunterModel theTypeHunterModel) { // POST: localhost:8080/api/type-hunter
entityManager.persist(theTypeHunterModel);
return theTypeHunterModel;
}
@Override
@Transactional
public TypeHunterModel update(TypeHunterModel theTypeHunterModel) { // PUT: localhost:8080/api/type-hunter
TypeHunterModel typeHunterModel = entityManager.merge(theTypeHunterModel);
return typeHunterModel;
}
}
Код: Выделить всё
TypeHunterREST.java
Код: Выделить всё
package com.user.springboot3.project.REST;
import com.user.springboot3.project.DAO.TypeHunterDAO;
import com.user.springboot3.project.Model.TypeHunterModel;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping("/api")
@RestController
public class TypeHunterREST {
private TypeHunterDAO typeHunterDAO;
public TypeHunterREST(TypeHunterDAO theTypeHunterDAO) {
this.typeHunterDAO = theTypeHunterDAO;
}
@PostMapping("/type-hunter")
public TypeHunterModel create(@RequestBody @Valid TypeHunterModel typeHunterModel) {
return typeHunterDAO.create(typeHunterModel);
}
@PutMapping("/type-hunter")
public TypeHunterModel update(@RequestBody @Valid TypeHunterModel typeHunterModel) {
return typeHunterDAO.update(typeHunterModel);
}
}
Подробнее здесь: https://stackoverflow.com/questions/781 ... -dont-work
Мобильная версия