Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Angular Tutorial: Working With Java and Spring on the Server-Side

DZone 's Guide to

Angular Tutorial: Working With Java and Spring on the Server-Side

Use Angular Datatables with different wrappers in Java's Spring framework, along with the DataTablesInput and DataTablesOutput methods when using custom queries.

· Web Dev Zone ·
Free Resource

For the Angular 5 client-side, check out the below repository:

https://l-lin.github.io/angular-datatables/#/basic/server-side-angular-way

Server-Side Development With Spring

In the Spring controller, write a wrapper called NewWrapper and use NewWrapperService:

@RequestMapping(value = "/get", method = RequestMethod.POST)
  public DataTablesOutput<NewWrapper> listPOST(@Valid @RequestBody DataTablesInput input) {
  return newWrapperService.findAll(input);
}

In the Spring service, wrap it in the way shown below. Change the DataTablesInput values, giving proper overrides of JSON input data and the DataTablesOutput values, using ModelMapper or your own mapper, finally overriding OldObjectPaginate. Write your own custom query for repository currentRepo.findAll.

@Transactional
public DataTablesOutput<NewWrapper> findAll(DataTablesInput input) {
  DataTablesInput input1 = new DataTablesInput();
  input1.setDraw(input.getDraw());
  input1.setOrder(input.getOrder());
  input1.setStart(input.getStart());
  input1.setLength(input.getLength());
  input1.setSearch(input.getSearch());
  List<Column> columns = input.getColumns();
  for(Column column:columns) {
    if (column.getData().equals("levelType")) {
    column.setData("id.levelType");
    }
    else if (column.getData().equals("levelCode")) {
    column.setData("id.levelCode");
    }
  }
  input1.setColumns(columns);
  DataTablesOutput<OldObjectPaginate> dataTableOutput = currentRepo.findAll(input1);
  DataTablesOutput<NewWrapper> dataTableResult = new DataTablesOutput<NewWrapper>();
  dataTableResult.setDraw(dataTableOutput.getDraw());
  dataTableResult.setRecordsFiltered(dataTableOutput.getRecordsFiltered());
  dataTableResult.setRecordsTotal(dataTableOutput.getRecordsTotal());
  /*TypeMap<OldObjectPaginate, NewWrapper> typeMap = modelMapper.getTypeMap(OldObjectPaginate.class, NewWrapper.class);
  if(typeMap==null) {
  modelMapper.addMappings(new PropertyMap<OldObjectPaginate, NewWrapper>() {
    @Override
    protected void configure() {
    map().setLevelCode(source.getId().getLevelCode());
    map().setLevelType(source.getId().getLevelType());
    }
  });
  }*/
  List<NewWrapper> newWrapperList = new ArrayList<>();
  for(OldObjectPaginate data: dataTableOutput.getData()) {
    NewWrapper newWrapper = new NewWrapper();
    newWrapper.setLevelCode(data.getId().getLevelCode());
    newWrapper.setLevelType(data.getId().getLevelType());
    newWrapperList.add(newWrapper);
  }
  //dataTableResult.setData(modelMapper.map(dataTableOutput.getData(), new TypeToken<List<NewWrapper>>() {}.getType()));
  dataTableResult.setData(newWrapperList);
  return dataTableResult;
}
Topics:
angular ,web dev ,java web development ,spring tutorial ,query databases

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}