방금 Grails 2.4.4로 작업을 시작했으며 JSON을 출력하는 REST API를 만들려고합니다. 그러나 객체를 JSON으로 렌더링 할 때 문제가 있습니다. 아래 나열된 것은 도메인 클래스, 컨트롤러 및 objectmarshall입니다.Grails 렌더링 JSON
도메인 클래스 :
@Resource(uri='/users')
class User {
List contacts;
String name;
String password;
static hasMany=[contacts:Contact]
static constraints = {
}
static mapping = {
contacts lazy: false
}
}
컨트롤러 : 그루비
class UserController {
def index() {
//json
render User.getAll() as JSON
}
부트 스트랩 : 나는 내 응용 프로그램을 처음 실행 한 후, 대신 JSON의하지만 경우 XML을 반환
class BootStrap {
def init = { servletContext ->
JSON.registerObjectMarshaller(User) {
def output = [:]
output['id'] = it.id
output['name'] = it.name
output['contacts'] = it.contacts
return output;
}
JSON.registerObjectMarshaller(Contact) {
def output = [:]
output['id'] = it.id;
output['name'] =it.name;
output['phoneNumber'] = it.phoneNumber;
output['userId'] = it.user.id;
return output;
}
}
}
(예를 들어 주석을 추가하면) JSON이 생성됩니다.
무엇이 누락 되었습니까?
사실 내 문제를 해결 한 것은 @Resource 주석을 제거하는 것이 었습니다. 어노테이션과'render as '메소드 사이에 어떤 종류의 충돌이 있었던 것으로 보인다. – stack404