• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

HATEOAS 관련 질문 드립니다.

18.11.28 13:56 작성 조회수 207

0

HATEOAS 관련 질문 드립니다.

강의 잘 보고 있습니다.

hateoas 관련 기능을 따라 배 보고 있는데.. 이 부분은 따라 해도 잘 되지 않네요

hateoas 기능을 사용하지 않는 경우에는 별 문제가 없는데..

예제에서 보여주신 방법으로 hateoas 기능을 추가 하면 아래와 같은 로그가 뜨고

화면도 아래와 같습니다.

뭐가 문제일까요??

바쁘시겠지만 답변 부탁 드립니다.

감사합니다.

<html>

<body>

<h1>Whitelabel Error Page</h1>

<p>

This application has no explicit mapping for /error, so you are seeing this as a fallback.

</p>

<div id="created">Wed Nov 28 13:42:39 KST 2018</div>

<div>

There was an unexpected error (type=Internal Server Error, status=500).

</div>

<div>

Could not marshal [PagedResource { content: [Resource { content: cmars.springboot.hateoas.Account@34829253, links: [] }, Resource { content: cmars.springboot.hateoas.Account@3902fe0, links: [] }, Resource { content: cmars.springboot.hateoas.Account@22ffbfaa, links: [] }, Resource { content: cmars.springboot.hateoas.Account@6cc2fe4e, links: [] }, Resource { content: cmars.springboot.hateoas.Account@4c72f98f, links: [] }, Resource { content: cmars.springboot.hateoas.Account@4e1f30ce, links: [] }, Resource { content: cmars.springboot.hateoas.Account@1142c448, links: [] }, Resource { content: cmars.springboot.hateoas.Account@6a8968a0, links: [] }, Resource { content: cmars.springboot.hateoas.Account@31ec5aff, links: [] }, Resource { content: cmars.springboot.hateoas.Account@642954c, links: [] }, Resource { content: cmars.springboot.hateoas.Account@1a56148, links: [] }, Resource { content: cmars.springboot.hateoas.Account@2048c5b4, links: [] }, Resource { content: cmars.springboot.hateoas.Account@73a058e0, links: [] }, Resource { content: cmars.springboot.hateoas.Account@61da5b2d, links: [] }, Resource { content: cmars.springboot.hateoas.Account@4b5fd97f, links: [] }, Resource { content: cmars.springboot.hateoas.Account@2d5cd0c5, links: [] }, Resource { content: cmars.springboot.hateoas.Account@5ccaead6, links: [] }, Resource { content: cmars.springboot.hateoas.Account@6f7ac151, links: [] }, Resource { content: cmars.springboot.hateoas.Account@5a5b79e0, links: [] }, Resource { content: cmars.springboot.hateoas.Account@114a4096, links: [] }], metadata: Metadata { number: 0, total pages: 2, total elements: 30, size: 20 }, links: [<http://localhost:8080/account?page=0&size=20>;rel="first", <http://localhost:8080/account?page=0&size=20>;rel="self", <http://localhost:8080/account?page=1&size=20>;rel="next", <http://localhost:8080/account?page=1&size=20>;rel="last"] }]: null; nested exception is javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: "org.springframework.hateoas.Resource" ??? ? ?????? ??? ? ???? ??? ???? ? ????.]

</div>

</body>

</html>

@RestController

public class AccountController {

@Autowired

private AccountRepository accountRepository;

@RequestMapping(value = "/account", method = RequestMethod.GET)

public PagedResources<Resource<Account>> accountAll(Pageable pageable, PagedResourcesAssembler<Account> assembler) {

int contNum = 10;

while (contNum > 0) {

Account account = new Account();

account.setAccountId(" 아디디 " + contNum);

account.setAccountName("이름 " + contNum);

accountRepository.save(account);

contNum--;

}

return assembler.toResource(accountRepository.findAll(pageable));

// 로그 --------------

[com.sun.istack.internal.SAXException2: "org.springframework.hateoas.Resource" 유형은 이 컨텍스트에서 인식할 수 없으므로 요소로 마셜링할 수 없습니다.]]

}

@RequestMapping(value = "/accounts", method = RequestMethod.GET)

public ResponseEntity accountAll(Pageable pageable) {

return new ResponseEntity<>(accountRepository.findAll(pageable), HttpStatus.OK);

}

// 정상출력

}

답변 2

·

답변을 작성해보세요.

0

답변 갑사합니다.

제가 원하는 답을 찾았습니다.

배경지식이 부족하다보니 조금만 예외 상황이 있어도 해결 하는데 많은 시간이 걸리네요

그래도 덕분에 많이 배우고 있습니다.

감사합니다.

0

질문을 HATEOAS 수업에 올려주셨으면 더 좋았겠네요. 제가 수업 시간에 한 것처럼 테스트 코드를 사용해서 요청을 만드셨다면 XML로 변환하려고 시도하지 않았을텐데, 웹 브라우저에서 요청을 하셨기 때문에 accept 헤더에 XML이나 HTML을 응답으로 받길 원한다는 정보가 들어가서, 스프링이 해당 데이터를 XML로 변환하려다 실패한 경우입니다.

간단하게 고치려면, @RequestMapping에 produces = MediaTypes.HAL_JSON_UTF8_VALUE 이 속성을 추가하시면 웹 브라우저에서 요청해도 JSON으로 응답을 만들어 줄겁니다.

질문 주셔서 감사합니다. XML로 변환할 때 실패하는 부분에 대해선 좀 더 살펴보겠습니다.