em không hiểu tại sao lại ko tìm thấty… ac nào giúp em với
Lỗi khi run project spring boot
Bạn đưa code DAO và Controller xem
Mà code không có tầng Service control business layer à?
Gợi ý:
- class implement DAO interface có annotation
@Repository
chưa? - Bạn thử
@Autowired
DAO interface thay vì class implement xem - Nếu không được nữa thì tạo contructor cho controller, truyền class implement xem
- Nhớ chỉnh sửa code xong gõ
mvn clean package
để rebuild lại app. Chạy file jarjava -jar <đường dẫn file jar>
thay vìmvn spring-boot:run
2 Likes
Controller
package com.itsol.controller;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import Dao.CategoryDaoImp;
import Model.Category;
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryDaoImp categoryDaoimp;
@GetMapping("/category")
public Page<Category> getCategory(Pageable pageable){
return categoryDaoimp.findAll(pageable);
}
@PostMapping("/category")
public Category createCategory(@Valid @RequestBody Category category) {
return categoryDaoimp.save(category);
}
@PutMapping("/category/{categoryId}")
public Optional<Category> updateCategory(@PathVariable Long categoryId,@Valid @RequestBody Category categoryRequest)
{
return categoryDaoimp.findById(categoryId).map(category->{category.setName(categoryRequest.getName());
return categoryDaoimp.save(category);});
}
@DeleteMapping("/category/{categoryId}")
public Optional<?> deleteCategory(@PathVariable Long categoryId){
return categoryDaoimp.findById(categoryId).map(category->{categoryDaoimp.delete(category);
return ResponseEntity.ok().build();});
}
}
em để cả @reponsitory với @autuwired cả rồi mà lúc chạy không được ạ
merged and moved by noname00
1 Like