To have a problem, in my controller when I have some entity with relationship it does not return the JSON, I already try to put the @JsonIgnore in the get of the references entity but nothing follows the code.
@Entity
@Table(name="tb_consumer")
public class Consumer implements Serializable{
private static final long serialVersionUID = 3474427923296664130L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String address;
private String city;
@Column(name="city_uf")
private String cityUf;
@Column(name="code_postal")
private int codePostal;
private String complement;
private int cpf;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="date_created")
private Date dateCreated;
private String name;
@Column(name="phone_cell")
private String phoneCell;
@Column(name="phone_home")
private String phoneHome;
@Column(name="phone_work")
private String phoneWork;
@ManyToMany(mappedBy="consumers")
private Set<Buy> buys = new HashSet<Buy>();
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="tb_status_id")
private Status status;
//getters e setters
My Controller:
@Controller
@RequestMapping(value = "/protected/users/consumers")
public class ConsumersController {
private static final String DEFAULT_PAGE_DISPLAYED_TO_USER = "0";
@Autowired
private ConsumerService consumerService;
@Autowired
private MessageSource messageSource;
@Value("5")
private int maxResults;
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> listAll(@RequestParam int page, Locale locale) {
return createListAllResponse(page, locale);
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView welcome() {
return new ModelAndView("consumersList");
}
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<?> create(@ModelAttribute("consumer") Consumer consumer,
@RequestParam(required = false) String searchFor,
@RequestParam(required = false, defaultValue = DEFAULT_PAGE_DISPLAYED_TO_USER) int page,
Locale locale) {
consumerService.save(consumer);
if (isSearchActivated(searchFor)) {
return search(searchFor, page, locale, "message.create.success");
}
return createListAllResponse(page, locale, "message.create.success");
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = "application/json")
public ResponseEntity<?> update(@PathVariable("id") int consumerId,
@RequestBody Consumer consumer,
@RequestParam(required = false) String searchFor,
@RequestParam(required = false, defaultValue = DEFAULT_PAGE_DISPLAYED_TO_USER) int page,
Locale locale) {
if (consumerId != consumer.getId()) {
return new ResponseEntity<String>("Bad Request", HttpStatus.BAD_REQUEST);
}
consumerService.save(consumer);
if (isSearchActivated(searchFor)) {
return search(searchFor, page, locale, "message.update.success");
}
return createListAllResponse(page, locale, "message.update.success");
}
@RequestMapping(value = "/{consumerId}", method = RequestMethod.DELETE, produces = "application/json")
public ResponseEntity<?> delete(@PathVariable("consumerId") int consumerId,
@RequestParam(required = false) String searchFor,
@RequestParam(required = false, defaultValue = DEFAULT_PAGE_DISPLAYED_TO_USER) int page,
Locale locale) {
try {
consumerService.delete(consumerId);
} catch (AccessDeniedException e) {
return new ResponseEntity<Object>(HttpStatus.FORBIDDEN);
}
if (isSearchActivated(searchFor)) {
return search(searchFor, page, locale, "message.delete.success");
}
return createListAllResponse(page, locale, "message.delete.success");
}
@RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> search(@PathVariable("name") String name,
@RequestParam(required = false, defaultValue = DEFAULT_PAGE_DISPLAYED_TO_USER) int page,
Locale locale) {
return search(name, page, locale, null);
}
private ResponseEntity<?> search(String name, int page, Locale locale, String actionMessageKey) {
ConsumerListVO consumerListVO = consumerService.findByNameLike(page, maxResults, name);
if (!StringUtils.isEmpty(actionMessageKey)) {
addActionMessageToVO(consumerListVO, locale, actionMessageKey, null);
}
Object[] args = {name};
addSearchMessageToVO(consumerListVO, locale, "message.search.for.active", args);
return new ResponseEntity<ConsumerListVO>(consumerListVO, HttpStatus.OK);
}
private ConsumerListVO listAll(int page) {
return consumerService.findAll(page, maxResults);
}
private ResponseEntity<ConsumerListVO> returnListToUser(ConsumerListVO consumerList) {
return new ResponseEntity<ConsumerListVO>(consumerList, HttpStatus.OK);
}
private ResponseEntity<?> createListAllResponse(int page, Locale locale) {
return createListAllResponse(page, locale, null);
}
private ResponseEntity<?> createListAllResponse(int page, Locale locale, String messageKey) {
ConsumerListVO consumerListVO = listAll(page);
addActionMessageToVO(consumerListVO, locale, messageKey, null);
return returnListToUser(consumerListVO);
}
private ConsumerListVO addActionMessageToVO(ConsumerListVO consumerListVO, Locale locale, String actionMessageKey, Object[] args) {
if (StringUtils.isEmpty(actionMessageKey)) {
return consumerListVO;
}
consumerListVO.setActionMessage(messageSource.getMessage(actionMessageKey, args, null, locale));
return consumerListVO;
}
private ConsumerListVO addSearchMessageToVO(ConsumerListVO consumerListVO, Locale locale, String actionMessageKey, Object[] args) {
if (StringUtils.isEmpty(actionMessageKey)) {
return consumerListVO;
}
consumerListVO.setSearchMessage(messageSource.getMessage(actionMessageKey, args, null, locale));
return consumerListVO;
}
private boolean isSearchActivated(String searchFor) {
return !StringUtils.isEmpty(searchFor);
}
}
My Vo
public class ConsumerListVO {
private int pageCount;
private long totalConsumers;
private String actionMessage;
private String searchMessage;
private List<Consumer> consumers;
private String systemUser;
private String status;
public ConsumerListVO(int pageCount, long totalConsumers, List<Consumer> consumers) {
this.pageCount = pageCount;
this.totalConsumers = totalConsumers;
this.consumers = consumers;
}
public String getSystemUser() {
return systemUser;
}
public void setSystemUser(String systemUser) {
this.systemUser = systemUser;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public void setConsumers(List<Consumer> consumers) {
this.consumers = consumers;
}
public ConsumerListVO() {
}