2010-12-07 5 views
2

저는 Spring MVC를 사용하고 있고 AJAX 호출을 사용하여 Person 객체 집합과 함께 JSON 메시지를 얻고 싶습니다. 나는이 jQuery 코드를 가지고 :스프링 MVC - AJAX/JSON - ResponseBody -> 서비스 호출

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

나는 서비스가 구현 한 모든 사람을 얻을 :

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

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

사람/allpersons (REST URL)는 RequestMapping 호출 이 서비스를 어떻게 호출 할 수 있습니까? 그래서 나는 대신에 놓아야 만합니까 ???

는이 같은 몇 가지를 시도,하지만 난 내 이클립스 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> 

어떤 제안이?

고맙습니다. & 최고 감사합니다. 당신의 방법은 사람에

답변

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 
    } 
} 

은}

+0

나는 그것을 변경하고 나는 서비스 호출을 한 번 더 오류 : 유형 IPersonService에서 비 정적 메소드 getAllPersons()에 대한 정적 참조를 만들 수 없습니다. 서비스 메소드는 이미 구현되었으며 이미 Adobe Flex 프론트 엔드 및 BlazeDS에서 사용했습니다. 하지만 자바 백엔드에서는 실행되지 않습니다. (공용 클래스 PersonServiceImpl은 IPersonService를 구현합니다 ... 존재합니다 ...) – Tim

+0

Answer updated updated 체크 – Vicky

+0

고맙습니다. 이제 오류 메시지가 나타납니다. IPersonServiceImpl을 유형으로 확인할 수 없습니다. 인터페이스의 구조와 구현은 옳습니다 (작성한 것과 같습니다). – Tim