Powered by Blogger.

API Integration and XML payload Handling

No comments :
API integration and XML payload handling in Java using HttpURLConnection and the Apache HttpClient library. To integrate with a client API using an API and XML-based payload, the entire process includes:
  1. ClientURLConfig.properties: Used to store client credential details Making an API call with an JSON/XML payload and pushing data communication.
  2. Data Communication from the two Client API using the JSON/XML payload API.
Step 1: ClientURLConfig.properties
 ####Example of Configure URL ############# 
CUST_URL=https://a143mk.com:443/a143mk1_test/MK/api/init_stage_interface/ CUST_USER_PASS_AUTH=useradmin:password

Step 2: Making an API call with an XML payload and pushing master data to the location.
  1. A REST API Controller is responsible for handling HTTP requests (such as GET, POST, PUT, DELETE) from clients and routing them to the appropriate service methods. It acts as the entry point for external clients to interact with the backend application. 
  2. Purpose: The main role of the controller is to manage incoming HTTP requests, map them to specific methods (often called endpoints), and return appropriate responses to the client. 
  3. Responsibilities: Handle HTTP requests (e.g., GET, POST, PUT, DELETE). Map the incoming requests to service methods. Return HTTP responses, often in JSON or XML format. Perform basic request validation or authentication. Act as the "interface" between the client and the service layer. In a Spring Boot application, for example, the controller is typically annotated with @RestController and the request mappings are handled using annotations like @GetMapping, @PostMapping, etc. 
Example of a REST Controller in Spring Boot with handlined XML Payload :
 
 
The Service class is a part of the business logic layer of your application. It contains methods that implement the core functionality of the application (e.g., creating, reading, updating, deleting data). The service class is called by the controller to perform operations that are not directly related to the web request itself. 
  1. Purpose: The service layer acts as a mediator between the controller and the data layer (such as a repository or database). It focuses on business logic and operations. 
  2. Responsibilities: Perform the core business logic of the application. Interact with the database (via repositories or DAOs). Provide methods for the controller to use (i.e., encapsulate business operations). Handle any necessary validation or transformation before sending data back to the controller. In a Spring Boot application, service classes are typically annotated with @Service, and they are injected into the controller using @Autowired (or constructor injection). 
Example of a Service Class in Spring Boot: 
 Method Internal Working ->getAllCustomerStores()->buildCustomerXmlPayload()->pushCustomerXmlToa143mkClient().
 


To test an API endpoint URL like https://a143mk.com/api/puchCustData/pushCustomerToClient using Postman, refer to the image. This will allow you to verify the API's functionality and inspect the response.



Cross-Origin Resource Sharing

No comments :
CROSS is a system that allows client web applications (such as JavaScript code) loaded on one domain to interact with resources located on a different domain.
Origin: An "Origin" (or source) URL consists of three parts: Protocol (scheme): Such as http or https. Domain (host): Such as a143mk.blogspot.com or localhost. Port (port number): Such as 8080 or 3000. If any of these three parts are different in a request, it is called a Cross-Origin request.
Example:
If your frontend (endpoints.com) sends a request to a backend API (api.com), it is Cross-Origin and will be intercepted by the browser. CORS is a way to relax this security restriction in a controlled manner, allowing legitimate cross-origin communication.
 

Configuring CORS globally in Spring Boot is the preferred approach, as it saves you from having to apply the @CrossOrigin annotation to every controller or method and keeps the configuration centralized.
Example: This method defines global CORS settings 
  1.  addMapping("/**"): This indicates that the CORS mapping will apply to all paths. 
  2.  allowedOrigins: Defines the client origins that are allowed to access your API. 
  3.  allowedMethods: The HTTP methods that are allowed. 
  4.  allowedHeaders: Allows all headers sent by the client. 
  5.  allowedCredentials: Set this to 'true' if the client is using cookies or HTTP authentication. 
  6.  maxAge: The time (in seconds) to cache the results of a pre-flight request.