Saturday, November 1, 2014

Web Services

  • REST API: This refers to any API that adheres to the principles of REST (Representational State Transfer). REST is an architectural style with constraints like statelessness, uniform interface, and resource-based interactions. Technically, an API can claim to be a REST API even if it doesn’t fully comply with all REST principles.

  • RESTful API: This term is used to describe APIs that strictly follow REST principles and are more closely aligned with RESTful design. Essentially, a RESTful API is a "better-behaved" REST API. It emphasizes proper implementation of REST constraints, including statelessness, resource representation via URLs, and the use of standard HTTP methods.

To simplify, all RESTful APIs are REST APIs, but not all REST APIs are RESTful.

REST - Point to point HTTP communication, not reliable, URL directly references the resource being accessed and message can be of any formats - only HTTP

JAX-RS - uses annotations like @Path, @GET, @POST, @PUT, @Consumes, @Produces and @Provider

SOAP - distributed communication, reliable, WSDL-XSD - contract based where WSDL will be registered in UDDI and standard XML message communication between WS client and server. - HTTP and SMTP

JAXB - API to marshal JAVA objects to XML and unmarshal it + Java web services development pack

Testing WS - Add the WS component in web.xml -> WS will be added to WS servlet. Now, we can test the WS using localhost.
WASAxis2Servlet is the servlet class name for WS.

WSDL Basic Parts - Definition, Type, Interface, Service, Endpoint, Message

WSDL minor parts - wsdl:definitions, types, message, part, portType, operation, input, output, binding, body, service, port

XSD parts - xsd:types, schema, element, complexType, sequence, element

UDDI - Universal Definition Discovery and Integration

. Set Up Your Project

  • Create a Spring Boot project using .

  • Include the dependency spring-boot-starter-web to enable web and REST API functionality.

2. Create a REST Controller

Use the @RestController annotation to define a RESTful controller. Here's an example:

java
@RestController
@RequestMapping("/api/users")
public class UserController {
    
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        // Logic to fetch a user by ID
        return userService.getUserById(id);
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        // Logic to create a new user
        return userService.saveUser(user);
    }

    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
        // Logic to update a user
        return userService.updateUser(id, user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        // Logic to delete a user
        userService.deleteUser(id);
    }
}

3. Key Annotations

  • @RestController: Combines @Controller and @ResponseBody to return JSON/XML responses.

  • @RequestMapping: Maps a base URL for the controller.

  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping: Handle specific HTTP methods.

  • @RequestBody: Binds the body of a request to an object.

  • @PathVariable: Binds a variable in the URL to a method parameter.

4. Handle Data

Spring Boot uses Jackson to handle JSON serialization and deserialization, ensuring that objects are converted seamlessly.

5. Database Integration (Optional)

  • Add spring-boot-starter-data-jpa for working with databases.

  • Configure the application.properties file for database connection details.

  • Use Spring Data JPA for CRUD operations.

6. Run and Test

  • Run your application using mvn spring-boot:run or directly from your IDE.

  • Test your APIs with tools like Postman or cURL.

7. Advanced Features

  • Validation: Use @Valid and validation annotations like @NotNull or @Size.

  • Exception Handling: Use @ControllerAdvice and @ExceptionHandler to provide meaningful error responses.

  • Security: Add spring-boot-starter-security for authentication and authorization

No comments:

Post a Comment