我对春天来说有点新鲜,而且我已经遇到了Null Pointer Exception。
我相信@Autowired
不适用于我的MongoRepository。
出于某种原因,当我尝试一些例子时,它正在运作。 (运行函数中注释掉的代码有效)
这是我得到的错误:
2016-05-20 02:31:20.877 ERROR 6272 --- [nio-8080-exec-2] o.a.c.c.C。[。[。[/]。[dispatcherServlet]:Servlet.service()for 在path []的上下文中的servlet [dispatcherServlet]引发了异常 [请求处理失败;嵌套异常是 带根本原因的java.lang.NullPointerException]
java.lang.NullPointerException:null at com.applesauce.service.CustomerService.addCustomer(CustomerService.java:24) 〜[类/:NA]
你们可以看看并指导我吗? 另外,如果我为最佳做法做错了,请告诉我。 如果您需要更多信息,请询问!
com.applesauce.controller
@RestController
@RequestMapping("/customer")
public class CustomerController {
private CustomerService customerService = new CustomerService();
@RequestMapping(value = "/addcustomer", method = RequestMethod.GET)
public Customer addCustomer(@RequestParam("firstName") String fName,
@RequestParam("lastName") String lName,
@RequestParam("email") String email,
@RequestParam("phoneNumber") String phoneNumber,
@RequestParam("source") String source){
return customerService.addCustomer(new Customer(fName,lName,email,phoneNumber,source));
}
}
com.applesauce.repository
@Repository
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
com.applesauce.service
@EnableMongoRepositories(basePackages = "com.applesauce.repository")
public class CustomerService {
@Autowired
private CustomerRepository repository;
public Customer addCustomer(Customer customer){
repository.save(customer);
return customer;
}
}
答案 0 :(得分:3)
Xtreme Biker意味着你应该为你的CustomerService添加@Service注释,如下所示:
@EnableMongoRepositories(basePackages = "com.applesauce.repository")
@Service
public class CustomerService {
...
}
此外,如果您希望Spring关注它,您永远不想使用new运算符创建服务。在CustomerController中,更改初始化行:
private CustomerService customerService = new CustomerService();
为:
@Autowired
private CustomerService customerService;
必须解决NullPointerException。
答案 1 :(得分:0)
I had the same issue and I solved this way (explanation is generic)
Repository.class
def move_perimeter(a, n, direction='clockwise'):
# a : input array
# n : number of elements to be moved
# direction : 'clockwise' or 'counterclockwise'
# Dictionary to map for number of elements rolling later
dirn_map = {'clockwise':n,'counterclockwise':-n}
# Store size
m,n = a.shape
r0 = np.arange(m-1)
r1 = np.arange(n-1)
# Get Top, Right, Bottom and Left side linear indices. Store those.
T = r1
R = n-1+r0*n
B = a.size-r1-1
L = n+r0[::-1]*n
idx = np.r_[T,R,B,L]
# Make a copy of input array
out = a.copy()
# Linearly index into output array with those original indices
# and extract rolled values from input array.
out.ravel()[idx] = a.ravel()[np.roll(idx, dirn_map[direction])]
return out
Service.class
In [233]: a
Out[233]:
array([[83, 13, 27, 13],
[90, 78, 57, 68],
[66, 47, 44, 53],
[13, 98, 95, 46],
[29, 87, 80, 92],
[91, 19, 86, 26],
[31, 27, 75, 72]])
In [234]: move_perimeter(a, n=1, direction='clockwise')
Out[234]:
array([[90, 83, 13, 27],
[66, 78, 57, 13],
[13, 47, 44, 68],
[29, 98, 95, 53],
[91, 87, 80, 46],
[31, 19, 86, 92],
[27, 75, 72, 26]])
In [235]: move_perimeter(a, n=2, direction='counterclockwise')
Out[235]:
array([[27, 13, 68, 53],
[13, 78, 57, 46],
[83, 47, 44, 92],
[90, 98, 95, 26],
[66, 87, 80, 72],
[13, 19, 86, 75],
[29, 91, 31, 27]])
Application.class
import play.api.libs.json.{Json, OFormat}
case class ResponseModel(content: NestedCaseClassModel)
object ResponseModel {
implicit val format: OFormat[ResponseModel] = Json.format
}
case class NestedCaseClassModel(value: String)
object NestedCaseClassModel {
implicit val format: OFormat[NestedCaseClassModel] = Json.format
}
答案 2 :(得分:0)
检查您是否已创建具有@Service批注的类的对象(使用new)。替代地,该类也应该自动装配,在这里您已经自动装配了存储库bean。