App.java,....
package AfFinal.AfFinal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
.....................................................................................
book.java,model
package AfFinal.AfFinal.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "books")
public class Book {
@Id
private String id;
private String name;
private String isbn;
private String author;
private Double price;
private int year;
private String publisher;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
.............................................................................
//bookRepository.java,model-interface
package AfFinal.AfFinal.model;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends MongoRepository<Book,String> {
}
...............................................................................
bookService,service
package AfFinal.AfFinal.Services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import AfFinal.AfFinal.model.Book;
import AfFinal.AfFinal.model.BookRepository;
@CrossOrigin(origins = "http://localhost:3000")
@RestController
@RequestMapping("/books")
public class BookService {
@Autowired
private BookRepository repository;
@SuppressWarnings("finally")
@RequestMapping(value ="/{idlist}" , method = RequestMethod.GET)
public Double getTotalPrice(@PathVariable String idlist) {
String[] idList = idlist.split(",");
double totalPrice = 0.0;
try {
for(int i = 0;i<idList.length;i++) {
totalPrice += this.findPrice(idList[i]);
}
} catch (Exception e) {
System.out.println(e);
} finally {
return totalPrice;
}
}
@SuppressWarnings("finally")
private Double findPrice(String id) {
Double price = 0.0;
try {
List<Book> bookList = repository.findAll();
for(Book book:bookList) {
if(book.getId().equals(id)) {
price =book.getPrice();
break;
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
return price;
}
}
}
.......................................................................................
application.properties
server.port=8083
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=user_demo
我已为单页面Web应用程序实现此代码。我有一个在大学实施图书馆系统的任务。我已经使用Springboot作为系统的后端和前端的反应。当我运行此代码时发现一些错误我已经以正确的方式放置我的代码请解释原因是什么。这是一个maven项目。有些代码是我实现的,有些代码是从互联网上复制的。我必须使用mongodb构建这个bu并作为前端做出反应。