我正在尝试使用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);
}
这是异常堆栈跟踪:
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
。
更新 我还提供了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
}
答案 0 :(得分:0)
你能尝试做这两点吗
<jpa:repositories base-package="com.acme.repositories"/>
。2我认为您应该从存储库自动装配中删除静态关键字,如果仅在此类中使用它,则将其设为私有,例如</ p>
@Autowired
private AlertRepository alertRepository;
答案 1 :(得分:0)
@Autowired
private AlertRepository alertRepository;
和 在您的主要应用程序类 加 @EnableAutoConfiguration
@SpringBootApplication
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
另外如果你没有使用@Repository,那么spring永远不会为你的repo创建bean,在这种情况下它会抛出nullPointer异常
@Repository
public interface AlertRepository extends
JpaRepository<Alert, Integer>{ }