Adding @deleteMapping to Spring Boot Rest service causes UnsatisifedDependencyException and NullPointerException

huangapple go评论61阅读模式
英文:

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 &#39;eligibleAddOnController&#39;: Unsatisfied dependency expressed through field &#39;eligibilityRepository&#39;; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;eligibilityRepository&#39; 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 &#39;eligibilityRepository&#39; 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 = &quot;DEALS&quot;)
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&lt;EligibleAddOn, String&gt; {
@Transactional
@Query(value = &quot;select * from DEALS &quot;
+ &quot;where CUST_ID IN &quot;
+ &quot;(select CUST_ID from TCS_CUSTOMER where msisdn = ?) &quot;
+ &quot;and ELIGIBILITY_FROM_DATE &lt; current_timestamp &quot;
+ &quot;and current_timestamp &lt; ELIGIBILITY_TO_DATE &quot;
+ &quot;ORDER BY DISPLAY_PRIORITY&quot;, nativeQuery = true)
List&lt;EligibleAddOn&gt; findByMsisdn(String msisdn);
@Modifying
@Transactional
@Query(value = &quot;delete from DEALS where &quot; +
&quot;CUST_ID in (select CUST_ID from TCS_CUSTOMER &quot; +
&quot;where MSISDN = :msisdn) and addon_name&quot; + &quot; = :addon_name&quot;, nativeQuery 
=true) void deleteAddOn(@Param(&quot;msisdn&quot;)String
msisdn, @Param(&quot;addon_name&quot;)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(&quot;/tcs&quot;)
public class EligibleAddOnController {
@Autowired
EligibilityRepository eligibilityRepository;
// Get a Single deal
@GetMapping(&quot;/eligibility-list/msisdn/{msisdn}&quot;)
public List&lt;EligibleAddOn&gt; getEligibleAddOnByMsisdn(@PathVariable(value = &quot;msisdn&quot;) String msisdn)
throws EligibleAddOnNotFoundException {
return eligibilityRepository.findByMsisdn(msisdn); // .orElseThrow(() -&gt; new
// EligibleAddOnNotFoundException(msisdn));
}
// Delete a deal
@DeleteMapping(&quot;/eligibility-list/msisdn/{msisdn}/radon/{addon_name}&quot;) public
ResponseEntity&lt;?&gt; deleteEligibleAddOn(@PathVariable(&quot;msisdn&quot;) String
msisdn,@PathVariable(&quot;addon_name&quot;) 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.

huangapple
  • 本文由 发表于 2023年3月23日 08:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定