我是 spring 的新手,但我正在嘗試制作一個 Web 應用程式,從中我可以將不同的條目添加到具有多個表的資料庫中 - 我有兩個表,現在,批處理和型別 - 我的型別代碼有效,我我使用一個小的python腳本對其進行了測驗。但是,當我嘗試進行批處理時(在這里我需要訪問型別表(Java 中的存盤庫)),我在存盤庫處獲得了一個 nullPointerExecption。
我搜索了多個地方以找到解決方案,但沒有一個對我有用。
以下是我用來創建新批次的不同類:
批處理控制器:
package org.example.BeerMachine.web;
import org.example.BeerMachine.data.models.Batch;
import org.example.BeerMachine.data.payloads.request.BatchRequest;
import org.example.BeerMachine.data.payloads.response.MessageResponse;
import org.example.BeerMachine.service.BatchService;
import org.example.BeerMachine.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/batch")
public class BatchController {
@Autowired
BatchService batchService;
@PostMapping("/add")
public ResponseEntity<MessageResponse> addBatch(@RequestBody BatchRequest batch){
MessageResponse newBatch = batchService.createBatch(batch);
return new ResponseEntity<>(newBatch, HttpStatus.CREATED);
}
}
批量服務:
package org.example.BeerMachine.service;
import org.example.BeerMachine.data.models.Batch;
import org.example.BeerMachine.data.payloads.request.BatchRequest;
import org.example.BeerMachine.data.payloads.response.MessageResponse;
import org.example.BeerMachine.data.repository.BatchRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
@Service
public class BatchServiceImpl implements BatchService {
@Autowired
BatchRepository batchRepository;
@Override
public MessageResponse createBatch(BatchRequest batchRequest) {
Batch newBatch = new Batch();
newBatch.setAmount(batchRequest.getAmount());
newBatch.setType(batchRequest.getType());
batchRepository.save(newBatch);
return new MessageResponse("New Batch created successfully");
}
}
BatchRequest 注意:在這里我得到了我的 nullPointerException:
package org.example.BeerMachine.data.payloads.request;
import org.example.BeerMachine.data.models.Type;
import org.example.BeerMachine.data.repository.TypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import javax.validation.constraints.NotNull;
import java.util.List;
@Component
public class BatchRequest {
@Autowired
private TypeRepository typeRepository; //This is null
@NotNull
private Integer amount;
@NotNull
private Integer type_id;
public Integer getAmount() {
return amount;
}
public Integer getType_id() {
return type_id;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Type getType() {
//Here is the nullPointerExecption
List<Type> typeList = typeRepository.findAll();
for (Type type : typeList) {
if (type.getId().equals(getType_id()))
return type;
}
return null;
}
public void setType(int t) {
this.type_id = t;
}
}
我的型別庫:
package org.example.BeerMachine.data.repository;
import org.example.BeerMachine.data.models.Type;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface TypeRepository extends JpaRepository<Type, Integer> {
//This was only added to test it - Saw it in another response. Didn't work
List<Type> findAll();
}
注意:我的 BatchRepository 具有相同的結構
如果有幫助,這是我的批處理模型類:
package org.example.BeerMachine.data.models;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name="batch")
public class Batch {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private Integer amount;
@ManyToOne(optional = false)
@JoinColumn(name = "type_id", nullable = false)
private Type type;
public Batch() {}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
@Override
public String toString() {
return "Batch{"
"id=" id
", amount=" amount
", type=" type
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Batch batch = (Batch) o;
return id.equals(batch.id) &&
amount.equals(batch.amount) &&
type == batch.type;
}
@Override
public int hashCode() {
return Objects.hash(id, amount, type);
}
}
如果您想了解我如何使用上面的代碼,請參閱下面的 python 腳本:
import requests
url1 = 'http://localhost:8081/batch/add'
batch = {'amount': 19,
'type_id': 6}
x = requests.post(url1, json = batch)
上面,A用戶輸入type_id,我的代碼需要根據id(資料庫中的id)對Type進行細化
最后一個注意事項:我有相同的型別類和幾乎相同的方法 - 不確定在這種情況下是否有用
uj5u.com熱心網友回復:
傳遞給控制器??的 BatchRequest 實體只是請求反序列化的結果。這意味著 spring 沒有在應用程式背景關系中創建它的實體,因此也沒有注入依賴項 (TypeRepository)。
您可以做的是TypeRepository在您的自動裝配中BatchServiceImpl并將其作為batchService.createBatch(batch,typeRepository)方法中的引數傳遞。
@Service
public class BatchServiceImpl implements BatchService {
@Autowired
private BatchRepository batchRepository; <----- Autowire here
@Autowired
private TypeRepository typeRepository;
@Override
public MessageResponse createBatch(BatchRequest batchRequest) {
Batch newBatch = new Batch();
newBatch.setAmount(batchRequest.getAmount());
newBatch.setType(batchRequest.getType(typeRepository)); <---- pass here
batchRepository.save(newBatch);
return new MessageResponse("New Batch created successfully");
}
}
uj5u.com熱心網友回復:
BatchRequest不應該是@Component,它應該是 POJO 而不是 Spring 管理的 Bean。正如@Ruelos Joel 所寫,BatchRequest batchRequest在您的createBatch()方法中是您為測驗代碼而執行的 POST 請求正文的反序列化結果。
寫完后,您BatchRequest應該如下所示:
public class BatchRequest {
@NotNull
private Integer amount;
@NotNull
private Integer type_id;
public Integer getAmount() {
return amount;
}
public Integer getType_id() {
return type_id;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public void setType_id(int t) {
this.type_id = t;
}
}
這意味著您在public Type getType()method中的邏輯應該放在其他地方,例如在 a 中TypeService:
@Service
public class TypeService {
@Autowired
private TypeRepository typeRepository;
public Type getType(int typeId) {
return typeRepository.findById(typeId);
}
}
您實際上不需要您擁有的邏輯,只是因為JpaRepository已經有一個方法可用于獲取給定 ID 的物體。
最后,您需要調整您BatchServiceImpl的使用新TypeService的如下:
@Service
public class BatchServiceImpl implements BatchService {
@Autowired
TypeService typeService;
@Autowired
BatchRepository batchRepository;
@Override
public MessageResponse createBatch(BatchRequest batchRequest) {
Batch newBatch = new Batch();
newBatch.setAmount(batchRequest.getAmount());
newBatch.setType(typeService.getType(batchRequest.getType_id()));
batchRepository.save(newBatch);
return new MessageResponse("New Batch created successfully");
}
}
作為一個側面說明,如果你想@NotNull擁有對驗證,您必須使用任何效果@Valid在你Controller如下:
@RestController
@RequestMapping("/batch")
public class BatchController {
@Autowired
BatchService batchService;
@PostMapping("/add")
public ResponseEntity<MessageResponse> addBatch(@Valid @RequestBody BatchRequest batch){
MessageResponse newBatch = batchService.createBatch(batch);
return new ResponseEntity<>(newBatch, HttpStatus.CREATED);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/342774.html
下一篇:通用型別串列的多執行緒排序
