开发者

Spring MVC - AJAX/JSON - ResponseBody -> calling a service

I am using Spring MVC and I want to do a AJAX call to get a JSON message with a Set of Person objects. I have this jQuery code:

$(document).ready(function() {
    getAllPersons();
});
function getAllPersons() {
    $.getJSON("person/allp开发者_JAVA技巧ersons", function(data) {
        alert(data);
    });
}

The person/allpersons (REST URL) calls a RequestMapping:

@RequestMapping(value="/allersons", method=RequestMethod.GET)
public @ResponseBody ??? ???() {
    ???
}

I have a service implemented to get all Persons:

public interface IPersonService {
    public Person addPerson(Person p);
    ...
    public Set<Person> getAllPersons();
}

How can I call this service? So what do I have to place instead of the ???

I tried several things like this, but I get errors in my Eclipse IDE:

public @ResponseBody <Set>Person getSomething() {
    Set<Person> persons = IPersonService.getAllPersons();
    return persons;
}

Errors / Warnings:

The type parameter Set is hiding the type Set<E>
Cannot make a static reference to the non-static method getAllPersons() from the type IPersonService
The type Set is not generic; it cannot be parameterized with arguments <Person>

Any suggestions?

Thank you in advance & Best Regards.


In your method Person is wrong it should be Set

public @ResponseBody Set<Person> getSomething() {
    Set<Person> persons = new IPersonServiceImpl().getAllPersons();
    return persons;
}

and another thing is you can not call interface method directly, first you need to implement that method in implementation class.

IPersonService.getAllPersons() This statement is wrong , here compiler considering it as static method getAllPersons() of IPersonService class.

public class IPersonServiceImpl implements IPersonService{
    public Set<Person> getAllPersons(){
     -- Your Business Logic
    }
    public Person addPerson(Person p){
     -- Your Business Logic
    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜