Spring MVC REST JSON

Asked

Viewed 281 times

0

Having a problem, in my controller when I have some entity with relationship it does not return me the JSON, already try to put the @Jsonignore get us the entity references but nothing, follow 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);
    }
}

Meu 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() {

    }

1 answer

1


Guys solved the problem,

Create a vo for each entity of mine

public class ConsumerVo{

    private int id;
    private String address;
    private String city;
    private String cityUf;
    private int codePostal;
    private String complement;
    private int cpf;
    private Date dateCreated;
    private String name;
    private String phoneCell;
    private String phoneHome;
    private String phoneWork;
    private Status status;

...getters e setters

My Stripe

public class ConsumerListVO {

    private int pageCount;
    private long totalConsumers;    
    private String actionMessage;
    private String searchMessage;
    private List<ConsumerVo> consumers;
...getters e setters

Flw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.