Posts

Showing posts with the label Integrating Angular with Java

Refactoring Angular and Java Application code

Image
  In This topic we will refactor our code for better readability. Refactor JAVA Code : Let's start with JAVA Code so you can see in previous few topic we have added cross origin annotation in each controller to call API from Angular application. what will happen if you have lots of Controller then there will a chance we miss adding it. So what we will do ? For now we will add a bean in  RestWebserviceApplication.class which will handle cross origin. We are going to use WebMvcConfigurer and will override addCorsMappings as per our project needs. WebMvcConfigurer : It is an Interface which is used to customized JAVA Configuration for spring MVC enabled project. addCorsMappings/ CorsRegistry   : It is a default method provided by  WebMvcConfigurer to configure global cross origin request. Update your code as shown below and once you add this you can remove CrossOrigin annotation from Controller class.  Check from UI if your application is working fine after this change. Refactor Angul

Retrieve Todo List and Integration with UI

Image
  In Previous few topic's we have created our java application and tried to connect JAVA service to Angular and we found when we click on button's our java API is getting called. we saw the use of HTTP client modules. In this topic we will start working on our todoList Application and create JAVA Services for below points: Retrieve all the todo list. Creating a todo Editing a todo Deleting a todo Before this quickly move HelloWorldController.class and HelloWorldBean.class into a new package : package com.learnangularwithjava.restwebservice.helloworld as shown below the project structure: Now create a new package com.learnangularwithjava.restwebservice.todoresources and create a new class TodoController.class in it. please see below screen shot of code. Note: Add @CrossOrigin(origins = "http://localhost:4200/")  Now once the code is done start your service by running RestWebserviceApplication.class and hit this URL http://localhost:8080/users/dummy/todos you will see b

Error Response and use of Path Variable

Image
  In previous topic we have integrated our java service with angular service and seen when clicking on welcome button our java API is getting called and success message is returned, in this article we will use the same JAVA method to return Runtime error and we will map this error using subscriber in angular application. let's start with updating our java API /hello-world-bean as below :  Now update your welcome.component.ts as below :  Now Refresh your page and click on button you can see the error response is coming as below:  Now we quickly add one more method in JAVA which take input from UI and return that from JAVA in response. Now add a method in HelloWorldController.java class as below, in this we are using path variable to take input. @PathVariable Annotation which indicates that a method parameter should be bound to a URI template variable. After adding this update your UI Code as Below:  Now refresh you page you will see below result when you click on welcome user name b

Integrating Angular Application with Restful API

Image
From this topic we will update our angular todoList Application with Restful API's. Let's start with our welcome page and add a button, on clicking of that button we will call our webservice. Angular provide us HTTP client module which we are going to use using observable. Before starting make sure you java and angular application is started. Now go to your welcome.component.html to add a button as show below After adding button you have to add getWelcomeMessage() method in your welcome.commponent.ts file and print something for now in console. now if check from your localhost you can see it will print in the console Now we will connect the java service for that let's create a service in angular which will connect to backend java service, for creating a service we will command ng generate service service/data/welcomeData. Now create a method in welcome-data.service.ts file you have to use this service in your welcome component by using dependency injection and update your g