`
sillycat
  • 浏览: 2492284 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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:
分享到:
评论

相关推荐

    Pro Spring for Integeration

    spring 用于 信息系统的集成 的最新英文书籍 著名的Apress 出版社 出版

    spring shiro cas

    spring shiro cas ! maven integeration

    WEB项目集成Flex3功能

    所谓万事开头难,共同期待好的向导,故在《FRAME-INTEGERATION》专栏,逐步整理并给出日常开发框架整合此类向导,而此类整合主要关注于Struts2、Spring2、iBatis2及Flex3的种种组合方式整合,而本篇整合仅仅是入门级...

    工作流人工任务 Websphere Integeration Development 开发

    工作流人工任务 Websphere Integeration Development 开发的实例, 部署到W P S上。 演示了2个人工任务的流程处理。

    docker-jenkins-integeration

    docker-jenkins-integeration

    Process Integration7.0标准的安装文档

    标准安装手册,很值得一看,一定要按照标准的东西来

    MuleStudio用户手册.doc

    Mule是一个以Java为核心的轻量级的消息框架和整合平台,基于EIP(Enterprise Integeration Patterns,由Hohpe和Woolf编写的一本书)而实现的。Mule的核心组件是UMO(Universal Message Objects,从Mule2.0开始UMO这一...

    ETL工具kettle学习总结.doc

    Kettle也叫PDI(全称是Pentaho Data Integeration),是一款开源的ETL工具,项目开始于2003年,2006年加入了开源的 BI 组织 Pentaho, 正式命名为PDI。

    kettle-spoon介绍

    Kettle是一款国外开源的ETL工具,纯java编写,可以在Window、Linux、Unix上运行,数据抽 ...Kettle 是 PDI 以前的名称,PDI 的全称是Pentaho Data Integeration,Kettle 本意是水壶的意思,表达了数据流的含义。

    bootstrap-easy-sidebar, 围绕主题集成和灵活性设计的Bootstrap 工具条.zip

    bootstrap-easy-sidebar, 围绕主题集成和灵活性设计的Bootstrap 工具条 bootstrap-easy-sidebar我看到了 Bootstrap的几个边栏,找到了大多数缺乏主题integeration和灵活性的领域。 通过继承导航栏的所有样式,然后在...

    ETL之PDI/Kettle9.x/8.x案例培训实战从入门到应用案例

    利用碎片化的时间学习即可,每天学习一小时或3个课时即可达到效果;时间充足者,自由安排 课程目标: 促使教育公平,打破教育壁垒,让ETL更加的大众化使用 课程简介: 【可视化介绍】 Kettle 是 PDI 以前的名称...

    i4C_Industry_4_0_Cell

    工业 4.0 单元 (i4C) - 根据工业 4.0 概念的机器人单元该项目的第一阶段涉及工业 4.0 背景下的机器人单元设计,并在布尔诺理工大学机械工程学院自动化与计算机科学研究所实施。 这一阶段项目的主要部分是单元的建设...

Global site tag (gtag.js) - Google Analytics