Hi I am new to Java And Spring Boot and everything related like JPA,
I receive a project that i need to
(A)receive(or get, not exactly streaming) data from a Client (not from REST approach), storing it into database ,
(B)and display it in web . using Spring Boot and JPA(jpa not mandatory)
i study few days and was successful in CRUD with database using rest approach (controller, service, repo, serviceimp) which many resource and tutorial demonstrates.
However, i have been struggling in part(A) to insert data to database using Spring Boot
So How can i insert some data into database with using controller/rest ?
My project was in below: file model
@Entity
@Table(name="Device")
public class Device {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long deviceId;
@Column(nullable = false,name = "deviceName",unique=true)
private String deviceName;
@Column(name = "regId")
private String registrationId;
@Column(name = "regDate")
private Date registrationDate;
// public Device(Long deviceId, String deviceName, String registrationId, Date registrationDate) {
// this.deviceId = deviceId;
// this.deviceName = deviceName;
// this.registrationId = registrationId;
// this.registrationDate = registrationDate;
// }
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getRegistrationId() {
return registrationId;
}
public void setRegistrationId(String registrationId) {
this.registrationId = registrationId;
}
public Date getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
}
@Override
public String toString() {
return "Device{" +
"deviceId=" + deviceId +
", deviceName='" + deviceName + '\'' +
", registrationId='" + registrationId + '\'' +
", registrationDate=" + registrationDate +
'}';
}
}
file DeviceRepository
@Repository
public interface DeviceRepository extends JpaRepository<Device,Long> {
//will cause error, the function name here must follow all the property
// public Boolean findDevicebyName(String deviceName);
Boolean existsBydeviceName(String deviceName);
}
for DeviceService and DeviceServiceImpl, will skip this for data insert part,
I was success in inserting into database using
rest appoarch
command line runner approach
but i need to run in normal application without REST what i have try was listed as below:
(TRY METHOD 1)
for File Rcv/Msgreceiver:
@Service
public class Msgreceiver {
@Autowired
private DeviceRepository deviceRepository;
@Autowired
public void msglisten() {
// System.out.println("why come here2");
Date date = new Date();
Device product = new Device();
product.setDeviceName("product 4567890");
// product.setDeviceId(3L);
product.setRegistrationDate(date);
product.setRegistrationId("b12345786789012");
System.out.println(product);
// // save product
deviceRepository.save(product);
System.out.println("why come here");
}
}
@RestController
@SpringBootApplication
public class SeverleshanV0Application {
public static void main(String[] args) {
SpringApplication.run(SeverleshanV0Application.class, args);
//
//FunctionB.printB();
//System.out.println("in a single end point");
Msgreceiver abc = new Msgreceiver();
abc.msglisten();
// Msg123 msgserver = new Msg123();
// msgserver.msglisten();
}
If i adding @Service to the Msgreceiver
this will run the the Msgreceiver TWICE and in the second time
this will raise because "this.deviceRepository" is null
i have check using "new" will cause problem ? but i have no idea of what should i do
- if i dont add @Service to Msgreceiver, or either delete the @Autowired this will also raise because "this.deviceRepository" is null
(3) if i move the file from Rcv/Msgreceiver to /Msgreceiver(without package) in parallel to the root application file, it runs completely ok.(but is it not suggest to do so?)
So it would great if someone point out the problem for me,
Thanks