[英]Null pointer exception spring boot jpa
我正在尝试使用jpa将值传递给springboot应用程序中的持久层。 但是,每次我看到一个空指针异常时,尽管我看到要持久化的对象格式正确。 我的代码片段如下。
AlertRepository
public interface AlertRepository extends JpaRepository<Alert, Integer>{
}
AlertController
@Controller
public class AlertController {
@Autowired
private AlertRepository alertRepository;
@GetMapping(path="/add") // Map ONLY GET Requests
public @ResponseBody String addNewAlert (@RequestParam Alert cAlert) {
Alert alert = new Alert();
alert.setAlert(cAlert.getAlert());
alert.setAttack(cAlert.getAttack());
alertRepository.save(alert);
.....
.....
return "Saved";
}
Get_Save_Alert
ApiResponse hh = zapClient.core.numberOfAlerts(target);
List<Alert> alertList = zapClient.getAlerts(target, 0, 0);
// zapClient.core.alerts(target, start, count);
System.out.println("the number of alerts is : " + hh);
de.cavas.model.Alert cavasAlert = new de.cavas.model.Alert();
for (Alert alert : alertList) {
cavasAlert.setRisk(alert.getRisk().toString());
cavasAlert.setConfidence(alert.getConfidence().toString());
cavasAlert.setUrl((alert.getUrl().toString()));
cavasAlert.setParam(alert.getParam().toString());
cavasAlert.setSolution(alert.getSolution());
cavasAlert.setCweid(String.valueOf(alert.getCweId()));
cavasAlert.setWascid(String.valueOf(alert.getWascId()));
cavasAlert.setAttack(alert.getAttack());
cavasAlert.setDescription(alert.getDescription());
cavasAlert.setName(alert.getName());
cavasAlert.setPluginId(alert.getPluginId());
cavasAlert.setReference(alert.getReference());
controller.addNewAlert(cavasAlert);
}
这是异常stacktrace:
Exception : null
java.lang.NullPointerException
at de.cavas.repository.AlertController.addNewAlert(AlertController.java:26)
at de.cavas.SecurityTest.preRegistrationTest(SecurityTest.java:281)
at
de.cavas.InstanceRegistry.handleTempRegistration(InstanceRegistry.java:250)
at de.cavas.InstanceRegistry.register(InstanceRegistry.java:155)
跟踪中的第26行alertRepository.save(alert)
AlertController Class
的行alertRepository.save(alert)
。
更新我还提供了application.yml文件,以防万一我看不到“鱼腥”!
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
spring:
datasource:
url: jdbc:mysql://sssss:500/vulncorrelate?useSSL=false
username: ss
password: ss
platform: mysql
initialize: false
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
generate-ddl: true
spring.jpa.show-sql: true
hibernate.ddl-auto: update
更新2- Alert
实体类
@Entity
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Alert {
String microserviceName;
String microservicePort;
String microserviceIpAddress;
String microserviceId;
String timeStamp;
@JsonProperty("sourceid")
private String sourceid;
@JsonIgnore
@JsonProperty("other")
private String other;
@JsonProperty("method")
private String method;
@Lob
@JsonProperty("evidence")
private String evidence;
@JsonProperty("pluginId")
private String pluginId;
@JsonProperty("cweid")
private String cweid;
@JsonProperty("confidence")
private String confidence;
@JsonProperty("wascid")
private String wascid;
@JsonProperty("description")
private String description;
@JsonProperty("messageId")
private String messageId;
@Lob
@JsonProperty("url")
private String url;
@Lob
@JsonProperty("reference")
private String reference;
@JsonProperty("solution")
private String solution;
@Lob
@JsonProperty("alert")
private String alert;
@Lob
@JsonProperty("param")
private String param;
@Lob
@JsonProperty("attack")
private String attack;
@JsonProperty("name")
private String name;
@JsonProperty("risk")
private String risk;
@JsonProperty("id")
private int id;
@JsonProperty("sourceid")
public String getSourceid() {
return sourceid;
}
public Alert(String microserviceName, String microservicePort, String
microserviceIpAddress, String microserviceId,
String timeStamp, String sourceid, String other, String method,
String evidence, String pluginId,
String cweid, String confidence, String wascid, String description,
String messageId, String url,
String reference, String solution, String alert, String param,
String attack, String name, String risk,
int id) {
super();
this.microserviceName = microserviceName;
.....
}
public Alert() {
// TODO Auto-generated constructor stub
}
...... //setters and getters
}
你能尝试做这两点吗
<jpa:repositories base-package="com.acme.repositories"/>
。 2我认为您应该从“存储库自动装配”中删除static关键字,如果仅在此分类中使用它,则将其设为私有
@Autowired
private AlertRepository alertRepository;
@Autowired
private AlertRepository alertRepository;
然后在您的主应用程序类上添加@EnableAutoConfiguration
@SpringBootApplication
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
另外,如果您不使用@Repository,那么spring将永远不会为您的仓库创建bean,在这种情况下,它将抛出nullPointer异常
@Repository
public interface AlertRepository extends
JpaRepository<Alert, Integer>{ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.