英文:
Adding @deleteMapping to Spring Boot Rest service causes UnsatisifedDependencyException and NullPointerException
问题
以下是您提供的代码的翻译:
Application.java:
package pack.psl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TcsApplication {
public static void main(String[] args) {
SpringApplication.run(TcsApplication.class, args);
}
}
Model.java:
package pack.model;
import javax.persistence.*;
import javax.validation.constraints.*;
@Entity
@Table(name = "DEALS")
public class EligibleAddOn {
@Id
@NotNull
private String deals_id;
@NotNull
private String cust_id;
@NotNull
private String addon_name;
@NotNull
private String eligibility_from_date;
@NotNull
private String eligibility_to_date;
public EligibleAddOn() {
super();
}
public String getDeals_id() {
return deals_id;
}
public void setDeals_id(String deals_id) {
this.deals_id = deals_id;
}
public String getCust_id() {
return cust_id;
}
public void setCust_id(String cust_id) {
this.cust_id = cust_id;
}
public String getAddon_Name() {
return addon_name;
}
public void setAddon_Name(String addon_Name) {
this.addon_name = addon_Name;
}
public String getEligibility_from_date() {
return eligibility_from_date;
}
public void setEligibility_from_date(String eligibility_from_date) {
this.eligibility_from_date = eligibility_from_date;
}
public String getEligibility_to_date() {
return eligibility_to_date;
}
public void setEligibility_to_date(String eligibility_to_date) {
this.eligibility_to_date = eligibility_to_date;
}
}
Repository.java:
package pack.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import pack.model.EligibleAddOn;
public interface EligibilityRepository extends JpaRepository<EligibleAddOn, String> {
@Transactional
@Query(value = "select * from DEALS "
+ "where CUST_ID IN "
+ "(select CUST_ID from TCS_CUSTOMER where msisdn = ?) "
+ "and ELIGIBILITY_FROM_DATE < current_timestamp "
+ "and current_timestamp < ELIGIBILITY_TO_DATE "
+ "ORDER BY DISPLAY_PRIORITY", nativeQuery = true)
List<EligibleAddOn> findByMsisdn(String msisdn);
@Modifying
@Transactional
@Query(value = "delete from DEALS where " +
"CUST_ID in (select CUST_ID from TCS_CUSTOMER " +
"where MSISDN = :msisdn) and addon_name" + " = :addon_name", nativeQuery
=true) void deleteAddOn(@Param("msisdn")String
msisdn, @Param("addon_name")String addon_name);
}
Controller.java:
package pack.controller;
import pack.exception.EligibleAddOnNotFoundException;
import pack.model.EligibleAddOn;
import pack.repository.EligibilityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import javax validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/tcs")
public class EligibleAddOnController {
@Autowired
EligibilityRepository eligibilityRepository;
@GetMapping("/eligibility-list/msisdn/{msisdn}")
public List<EligibleAddOn> getEligibleAddOnByMsisdn(@PathVariable(value = "msisdn") String msisdn)
throws EligibleAddOnNotFoundException {
return eligibilityRepository.findByMsisdn(msisdn);
}
@DeleteMapping("/eligibility-list/msisdn/{msisdn}/radon/{addon_name}")
public ResponseEntity<?> deleteEligibleAddOn(@PathVariable("msisdn") String msisdn,@PathVariable("addon_name") String addon_name){
eligibilityRepository.deleteAddOn(msisdn, addon_name);
return ResponseEntity.ok().build();
}
}
Application.properties:
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url= datasource
spring.datasource.username= username
spring.datasource.password= password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
server.error.include-stacktrace = always
如果您需要更多帮助或其他翻译,请随时提出。
英文:
I am making a Spring Boot Rest service and have encountered an issue when I add in a delete mapping. Before I added it the code worked fine and ran without issue, now when I try and compile it in the shell using mvn clean package I get the below error.
The error:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eligibleAddOnController': Unsatisfied dependency expressed through field 'eligibilityRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eligibilityRepository' defined in pack.psl.repository.EligibilityRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void pack.psl.repository.EligibilityRepository.deleteAddOn(java.lang.String,java.lang.String); Reason: null; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eligibilityRepository' defined in nz.one.psl.repository.EligibilityRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void pack.psl.repository.EligibilityRepository.deleteAddOn(java.lang.String,java.lang.String); Reason: null; nested exception is java.lang.NullPointerException
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void pack.psl.repository.EligibilityRepository.deleteAddOn(java.lang.String,java.lang.String); Reason: null; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException
And this is my code;
Application:
package pack.psl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TcsApplication {
public static void main(String[] args) {
SpringApplication.run(TcsApplication.class, args);
}
}
Model:
package pack.model;
import javax.persistence.*;
import javax.validation.constraints.*;
@Entity
@Table(name = "DEALS")
public class EligibleAddOn {
@Id
@NotNull
private String deals_id;
@NotNull
private String cust_id;
@NotNull
private String addon_name;
@NotNull
private String eligibility_from_date;
@NotNull
private String eligibility_to_date;
public EligibleAddOn() {
super();
}
public String getDeals_id() {
return deals_id;
}
public void setDeals_id(String deals_id) {
this.deals_id = deals_id;
}
public String getCust_id() {
return cust_id;
}
public void setCust_id(String cust_id) {
this.cust_id = cust_id;
}
public String getAddon_Name() {
return addon_name;
}
public void setAddon_Name(String addon_Name) {
this.addon_name = addon_Name;
}
public String getEligibility_from_date() {
return eligibility_from_date;
}
public void setEligibility_from_date(String eligibility_from_date) {
this.eligibility_from_date = eligibility_from_date;
}
public String getEligibility_to_date() {
return eligibility_to_date;
}
public void setEligibility_to_date(String eligibility_to_date) {
this.eligibility_to_date = eligibility_to_date;
}
}
Repository:
package pack.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import pack.model.EligibleAddOn;
public interface EligibilityRepository extends JpaRepository<EligibleAddOn, String> {
@Transactional
@Query(value = "select * from DEALS "
+ "where CUST_ID IN "
+ "(select CUST_ID from TCS_CUSTOMER where msisdn = ?) "
+ "and ELIGIBILITY_FROM_DATE < current_timestamp "
+ "and current_timestamp < ELIGIBILITY_TO_DATE "
+ "ORDER BY DISPLAY_PRIORITY", nativeQuery = true)
List<EligibleAddOn> findByMsisdn(String msisdn);
@Modifying
@Transactional
@Query(value = "delete from DEALS where " +
"CUST_ID in (select CUST_ID from TCS_CUSTOMER " +
"where MSISDN = :msisdn) and addon_name" + " = :addon_name", nativeQuery
=true) void deleteAddOn(@Param("msisdn")String
msisdn, @Param("addon_name")String addon_name);
}
Controller:
package pack.controller;
import pack.exception.EligibleAddOnNotFoundException;
import pack.model.EligibleAddOn;
import pack.repository.EligibilityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/tcs")
public class EligibleAddOnController {
@Autowired
EligibilityRepository eligibilityRepository;
// Get a Single deal
@GetMapping("/eligibility-list/msisdn/{msisdn}")
public List<EligibleAddOn> getEligibleAddOnByMsisdn(@PathVariable(value = "msisdn") String msisdn)
throws EligibleAddOnNotFoundException {
return eligibilityRepository.findByMsisdn(msisdn); // .orElseThrow(() -> new
// EligibleAddOnNotFoundException(msisdn));
}
// Delete a deal
@DeleteMapping("/eligibility-list/msisdn/{msisdn}/radon/{addon_name}") public
ResponseEntity<?> deleteEligibleAddOn(@PathVariable("msisdn") String
msisdn,@PathVariable("addon_name") String addon_name){
eligibilityRepository.deleteAddOn(msisdn, addon_name);
return ResponseEntity.ok().build(); }
}
Application.properties
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url= datasource
spring.datasource.username= username
spring.datasource.password= password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
server.error.include-stacktrace = always
答案1
得分: 0
问题实际上出在 Spring Boot 本身,而不是代码。
根据这个链接 https://github.com/spring-projects/spring-boot/issues/34363,将我的项目从 2.7.9 降级到 2.6.6 解决了问题。
英文:
Turns out the issue is with spring boot itself rather than the code.
As per this https://github.com/spring-projects/spring-boot/issues/34363 downgrading my project from 2.7.9 to 2.6.6 resolved the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论