我正在尝试将HATEOAS与Spring boot和MongoTemplate一起使用。当我不使用HATEOAS(即不继承自ResourceSupport)时,它工作正常。但是,当我从实体类扩展ResourceSupport时,我根本无法提取实体列表。它给出了NullPointerException。这是代码。
SpringMongoConfig.java
package com.mindtree.learnmongo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.mongodb.MongoClient;
@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {
@Override
public MongoClient mongoClient() {
return new MongoClient();
}
@Override
protected String getDatabaseName() {
// TODO Auto-generated method stub
return "studentdb";
}
/*
public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(), "studentdb");
}*/
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
}
}
控制器类:
HelloController.java
package com.mindtree.learnmongo.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.mindtree.learnmongo.model.User;
import com.mindtree.learnmongo.service.UserService;
@RestController
@CrossOrigin
public class HelloController extends AbstractController {
@Autowired
private UserService userService;
@GetMapping(value = "/", produces = {"application/json; charset=utf-8"})
public List<User> getAllUser(){
List<User> u = this.userService.getAllUser();
//System.out.println(this.userService.getAllUser());
u.forEach(user -> {
user.add(ControllerLinkBuilder.linkTo(HelloController.class).slash(user.getUserId()).withSelfRel());
});
return u;
//return u;
//this.userService.getAllUser().forEach(u -> u.add(linkTo(methodOn(HelloController.class).getUserById(u.getUserid())).withSelfRel()));
//return this.userService.getAllUser();
/*
List<User> u = this.userService.getAllUser();
List<Resource<User>> res = new ArrayList<Resource<User>>();
*/
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
return null;
}
}
实体类
package com.mindtree.learnmongo.model;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceSupport;
@Document(collection = "users")
public class User extends ResourceSupport {
@Id
private String id;
String username;
String password;
public User() {
super();
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUserId() {
return id;
}
public void setUserId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
服务类别
package com.mindtree.learnmongo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Service;
import com.mindtree.learnmongo.config.SpringMongoConfig;
import com.mindtree.learnmongo.model.User;
@Service
public class UserService {
@Autowired
private SpringMongoConfig springMongoConfig;
public int haha = 34;
public List<User> getAllUser() {
try {
System.out.print("haha");
MongoOperations mongoOperation = this.springMongoConfig.mongoTemplate();
System.out.print("haha");
List<User> lst = mongoOperation.findAll(User.class);
System.out.println(lst+" hahaha");
return lst;
} catch (Exception e) {
System.out.println(e+" HAHA EXCEPTION");
return null;
}
}
}
主要的。
package com.mindtree.learnmongo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LearnmongoApplication {
public static void main(String[] args) {
SpringApplication.run(LearnmongoApplication.class, args);
}
}
有很多评论。一直在尝试一切。什么都没用。