Spring MVC - AJAX / JSON - ResponseBody - >打电话给服务

时间:2010-12-07 12:08:48

标签: java ajax json spring spring-mvc

我正在使用Spring MVC,我想进行AJAX调用以获取带有一组Person对象的JSON消息。 我有这个jQuery代码:

$(document).ready(function() {
    getAllPersons();
});
function getAllPersons() {
    $.getJSON("person/allpersons", function(data) {
        alert(data);
    });
}

person / allpersons(REST URL)调用RequestMapping:

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

我实施了一项服务以获取所有人员:

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

如何拨打此服务?那么我需要放置什么而不是???

我尝试过这样的几件事,但是我在Eclipse IDE中遇到错误:

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

错误/警告:

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>

有什么建议吗?

提前谢谢你&amp;最诚挚的问候。

1 个答案:

答案 0 :(得分:3)

在你的方法中,人是错的,应该设置

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

另一件事是你不能直接调用接口方法,首先你需要在实现类中实现该方法。

IPersonService.getAllPersons()这句话是错误的,这里编译器将其视为getAllPersons()类的静态方法IPersonService

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

}