Spring3 and REST Integeration(VIII)REST ERROR Handler and JSON Mapper Infinite

Spring3 and REST Integeration(VIII)REST ERROR Handler and JSON Mapper Infinite

First, how can I handle the REST error message in my REST API.
There is an example. In my get method in Person Conroller, I try to make a throw Exception like this:
if (id.equalsIgnoreCase("13")) {
     thrownew JsonServiceException("101",
     "Id can not be 13. Because I do not like it.");
}

The JsonServiceException will be a simple exception class extends Exception:
package com.sillycat.easyrestserver.exception;

publicclass JsonServiceException extends Exception {

privatestaticfinallongserialVersionUID = -2949102718021710130L;

private String errorCode;
private String errorMessage;

public JsonServiceException(String errorCode, String errorMessage) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}

public String getErrorCode() {
returnerrorCode;
}

publicvoid setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

public String getErrorMessage() {
returnerrorMessage;
}

publicvoid setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}

The handler in controller will be as follow:
@ExceptionHandler(JsonServiceException.class)
publicvoid handleJsonServiceException(JsonServiceException exception,
HttpServletResponse response) throws IOException {
     response.sendError(HttpServletResponse.SC_NOT_FOUND,
          exception.getErrorMessage());
}

The test implementation will be as follow:
publicvoid throwJSONError() throws Exception {
     person.setId(13);
     Mockito.when(mockPersonService.get(13)).thenReturn(person);

     MockMvcBuilders
     .standaloneSetup(personController)
     .build()
     .perform(
     MockMvcRequestBuilders.get("/person/13").accept(
     MediaType.APPLICATION_JSON))
     .andExpect(status().isNotFound());
}

I make my objects like these, person will have one company, but company will have many persons. The mock object will be as follow:
person = new Person(3, "person3");
Person person1 = new Person(1, "person1");

Company company1 = new Company(1, "company1");

List<Person> personList = new ArrayList<Person>();
personList.add(person);
personList.add(person1);
company1.setPersons(personList);

person.setCompany(company1);

I execute the same junit test case, I will get these error messages.
error message:
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.sillycat.easyrestserver.model.Company["persons"]->java.util.ArrayList[0]->com.sillycat.easyrestserver.model.Person["company"]->com.sillycat.easyrestserver.model.Company["persons"]->java.util.ArrayList[0]->com.sillycat.easyrestserver.model.Person["company"]-

That is because of infinite json mapper here. So the solution for these kind of problem will be the annotation supported in latest jackson lib.
@JsonBackReference("Company-Person")
public Company getCompany() {
     returncompany;
}

and

@JsonManagedReference("Company-Person")
public List<Person> getPersons() {
     returnpersons;
}

That is it.

references:

猜你喜欢

转载自sillycat.iteye.com/blog/1480505
今日推荐