14.1.1 • Published 2 years ago

ng-spring-boot v14.1.1

Weekly downloads
46
License
MIT
Repository
github
Last release
2 years ago

ng-spring-boot

Integrate Spring Boot Rest services to your Angular App.

Installation

npm i ng-spring-boot

Usage

Supported Operations:

  • GET with pagination
  • POST a new resource and read the location-header and new resource-id

Example

GET with pagination

Spring Boot Rest-Controller (CustomerService.java):

@RestController
@RequestMapping("/customers")
@CrossOrigin(origins = "*")
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @RequestMapping(method = RequestMethod.GET)
    public Page<Customer> listCustomers(@RequestParam(defaultValue = "") String filter, Pageable p) {
        if(filter.length() > 0) {
            return customerRepository.findByNameContaining(filter, p);
        }
        return customerRepository.findAll(p);
    }
}

Angular CustomerServce (customer.service.ts):

import { NgSpringBootService, Page, Pageable } from 'ng-spring-boot';

@Injectable({
  providedIn: 'root'
})
export class CustomerService extends NgSpringBootService {

  constructor(http: HttpClient) {
    super(http);
  }

  listCustomers(pageable?: Pageable): Observable<Page<Customer>> {
    return super.getPage<Customer>('http://localhost:8080/customers', pageable);
  }
}

Using CustomerService (customer-list.component.ts):

@Component({
  selector: 'app-customer-list',
  templateUrl: './customer-list.component.html',
  styleUrls: ['./customer-list.component.scss']
})
export class CustomerListComponent implements OnInit {

  page: Page<Customer>;

  constructor(private customerService: CustomerService) {
  }

  ngOnInit() {
    this.customerService.listCustomers({sort: 'name'}).subscribe(p => this.page = p);
  }

}

customer-list.component.html:

<div *ngIf="page">
  <ul>
    <li *ngFor="let customer of page.content">{{customer.name}}</li>
  </ul>
</div>

POST a new resource

Spring Boot Rest-Controller (CustomerService.java):

@RestController
@RequestMapping("/customers")
@CrossOrigin(origins = "*")
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity addCustomers(@RequestBody Customer customer) {
        Integer newCustomerId = customerRepository.saveAndFlush(customer).getId();
        return ResponseEntity
                .status(HttpStatus.CREATED)
                .header("Location", recipeId + "/customers/" + newCustomerId)
                .build();
    }
}

To enable the Location header with CORS, you need additional configuration:

@Configuration
public class WebConfig{

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("PUT", "POST", "DELETE")
                        .allowedHeaders("Location", "Content-Type")
                        .exposedHeaders("Location", "Content-Type");
            }
        };
    }
}

Usage (customer.service.ts)

  saveCustomer(customer: Customer): Observable<number> {
    return super.postResource('http://localhost:8080/customers', customer).pipe(
      switchMap(r => {
        return of(+r.id);
      })
    );
  }
14.1.1

2 years ago

1.8.0

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago