问题
I am completely new to Java. I am trying to display JSON data, for that I have decided to go for Jackson library. But I am getting the Error.
I am using
jackson-annotations-2.3.0.jar
jackson-core-2.4.2.jar
jackson-databind-2.4.2.jar
This is my Object and in this I have one dependency "tnStudentLocLog"
public class Student implements java.io.Serializable, WorkItem {
private int studentId;
private Date date;
private Date startTime;
private Date endtime;
private int room;
private Set tnStudentLocLog;
public Student() {
}
public Student(int studentId,Date date, int room, Date startTime, Date endtime) {
this.studentId = studentId;
this.date = date;
this.room = room;
this.startTime = startTime;
this.endtime = endtime;
}
}
UserController class:
@Controller
@RequestMapping(value="/students")
public class StudentController {
private static Logger logger = Logger.getLogger( StudentController.class);
private StudentManagerDelegate studentDelegate;
public StudentController() throws Exception
{
studentDelegate= new StudentManagerDelegate(ServiceType.LOCAL);
}
/********* GET ALL STUDENTS ************/
@RequestMapping(method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody SuccessResponse<List<Student>> getAllStudents() throws Exception {
Map<String,List<Student>> studentsMap = new HashMap<String,List<Student>>();
SuccessResponse<List<Student>> resp = new SuccessResponse<List<Student>>();
resp.list = studentDelegate.load();
return resp;
}
Here, I am getting the correct result. But, "studentId":2 is looping itself like below
{"max":"30","list":[{"studentId":2,"date":1339327730000,"startTime":1339327806000,"endtime":1339329470000,"room":0,"tnStudentLocLog":
[{"id":"studentId":2,"inTime":1339327872000},"sequenceId":2,"outTime":1339327967000,"Student":{"studentId":2,"date":1339327730000,"startTime":1339327806000,"endtime":1339329470000,"tnStudentLocLog":[{"id":{"studentId":2,"room":10,"inTime":1339327872000},"sequenceId":2,"outTime":1339327967000,"Student":{"studentId":2,"date":1339327730000,"startTime":1339327806000,"endtime":1339329470000,"tnStudentLocLog":[{"id":.......
But, In the URL when I enter "/students/4". It is displaying correctly. I don't know what exactly happening. I saw many posts, they are changing versions of Jackson core, annotations. But, that is not working in my case.
Thanks in advance for your help.
回答1:
Check do you have any cyclic dependency, if you have cyclic dependency you can ignore it by @JsonIgnore Check here for cyclic dependency
回答2:
Welcome to java. One of the first things you'll need to learn when using Java is how to set up and use the CLASSPATH. The "Class Not Found" error means one or more of your jar files are not in the classpath. Java does not implicitly look anywhere (not even the same subdirectory where your classes are) for .jar files. You have to tell it where to look. How do you set up the CLASSPATH? That depends on your platform (Windows, Linux, IIS, Apache, Tomcat, Bluejay, Eclipse, etc...) Learn that well, you will prevent many a headache.
回答3:
You are missing a jar file which would contain the needed class which is com.fasterxml.jackson.databind.JsonMappingException
You can download it from here and include in your Class Path.
来源:https://stackoverflow.com/questions/27358028/caused-by-java-lang-noclassdeffounderror-com-fasterxml-jackson-databind-jsonma