From bcf4f62d1a0cd16bef6a64f56a875253da0f2498 Mon Sep 17 00:00:00 2001 From: MattWay224 Date: Wed, 26 Mar 2025 01:16:01 +0300 Subject: [PATCH 1/3] swagger ui added --- build.gradle.kts | 1 + .../com/m/linshor/config/SwaggerConfig.java | 17 +++++++ .../controllers/LinShorController.java | 49 ++++++++++++++----- 3 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/m/linshor/config/SwaggerConfig.java diff --git a/build.gradle.kts b/build.gradle.kts index e31ffa8..9b3ab58 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -49,6 +49,7 @@ dependencies { annotationProcessor("org.projectlombok:lombok") testImplementation("org.springframework.boot:spring-boot-starter-test") testRuntimeOnly("org.junit.platform:junit-platform-launcher") + implementation ("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2") } diff --git a/src/main/java/com/m/linshor/config/SwaggerConfig.java b/src/main/java/com/m/linshor/config/SwaggerConfig.java new file mode 100644 index 0000000..3de81fa --- /dev/null +++ b/src/main/java/com/m/linshor/config/SwaggerConfig.java @@ -0,0 +1,17 @@ +package com.m.linshor.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; + +@Configuration +public class SwaggerConfig { + @Bean + public OpenAPI customOpenAPI() { + return new OpenAPI() + .info(new Info().title("LinShor URL Shortener API") + .version("1.0") + .description("API documentation for the URL shortener service")); + } +} \ No newline at end of file diff --git a/src/main/java/com/m/linshor/controllers/LinShorController.java b/src/main/java/com/m/linshor/controllers/LinShorController.java index df51612..3586ba5 100644 --- a/src/main/java/com/m/linshor/controllers/LinShorController.java +++ b/src/main/java/com/m/linshor/controllers/LinShorController.java @@ -3,6 +3,12 @@ import com.m.linshor.entities.Mapping; import com.m.linshor.services.LinShorService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; + import java.net.URI; import java.net.URL; import java.util.Optional; @@ -15,27 +21,40 @@ @RestController @RequestMapping("/linshor/v1") @AllArgsConstructor +@Tag(name = "URL Shortener API", description = "Operations for URL shortening and redirection") public class LinShorController { private final LinShorService linShorService; - @GetMapping("/find/{shortUrl}") - public Optional findByShortUrl(@PathVariable String shortUrl) { - return linShorService.findByShortUrl(shortUrl); - } - - @PutMapping("update") - public Mapping updateLink(String longUrl) { + @PutMapping("/update") + @Operation(summary = "Update a long URL", + description = "Updates an existing shortened URL with a new long URL.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Successfully updated"), + @ApiResponse(responseCode = "400", description = "Invalid URL format") + }) + public Mapping updateLink(@RequestParam @Parameter(description = "New long URL") String longUrl) { return linShorService.updateLink(longUrl); } @DeleteMapping("delete/{shortUrl}") - public int deleteByShortUrl(@PathVariable String shortUrl) { + @Operation(summary = "Delete a shortened URL", description = "Removes a shortened URL from the database.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Successfully deleted"), + @ApiResponse(responseCode = "404", description = "Short URL not found") + }) + public int deleteByShortUrl(@PathVariable @Parameter(description = "Short URL to delete") String shortUrl) { linShorService.deleteByShortUrl(shortUrl); return 200; } @PostMapping("/post") - public ResponseEntity linshorUrl(@RequestBody String longUrl, HttpServletRequest request) { + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Short URL created"), + @ApiResponse(responseCode = "400", description = "Invalid input") + }) + public ResponseEntity linshorUrl(@RequestBody @Parameter( + description = "The long URL to shorten", required = true) + String longUrl, HttpServletRequest request) { Mapping mapping = linShorService.saveLink(longUrl); String serverAddress = request.getRequestURL().toString().replace(request.getRequestURI(), ""); @@ -45,11 +64,19 @@ public ResponseEntity linshorUrl(@RequestBody String longUrl, HttpServle } @GetMapping("/{shortUrl}") - public ResponseEntity redirectToLongUrl(@PathVariable String shortUrl) { + @Operation(summary = "Redirect to long URL", + description = "Finds the original URL for a given short URL and redirects.") + @ApiResponses({ + @ApiResponse(responseCode = "302", description = "Redirected successfully"), + @ApiResponse(responseCode = "404", description = "Short URL not found"), + @ApiResponse(responseCode = "400", description = "Invalid URL stored in database") + }) + public ResponseEntity redirectToLongUrl(@PathVariable @Parameter( + description = "Short URL to resolve") String shortUrl) { Optional mapping = linShorService.findByShortUrl(shortUrl); if (mapping.isPresent()) { - String longUrl = mapping.get().getLongUrl().trim().replaceAll("^\"|\"$", ""); // Remove surrounding quotes + String longUrl = mapping.get().getLongUrl().trim().replaceAll("^\"|\"$", ""); try { URI uri = new URL(longUrl).toURI(); // Convert URL to URI safely From 47cbc87df3ebbbf93cc8138f5baa9ed1d6654706 Mon Sep 17 00:00:00 2001 From: MattWay224 Date: Wed, 26 Mar 2025 01:25:13 +0300 Subject: [PATCH 2/3] reformat --- src/main/java/com/m/linshor/config/SwaggerConfig.java | 6 +++--- .../java/com/m/linshor/controllers/LinShorController.java | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/m/linshor/config/SwaggerConfig.java b/src/main/java/com/m/linshor/config/SwaggerConfig.java index 3de81fa..27310f7 100644 --- a/src/main/java/com/m/linshor/config/SwaggerConfig.java +++ b/src/main/java/com/m/linshor/config/SwaggerConfig.java @@ -10,8 +10,8 @@ public class SwaggerConfig { @Bean public OpenAPI customOpenAPI() { return new OpenAPI() - .info(new Info().title("LinShor URL Shortener API") - .version("1.0") - .description("API documentation for the URL shortener service")); + .info(new Info().title("LinShor URL Shortener API") + .version("1.0") + .description("API documentation for the URL shortener service")); } } \ No newline at end of file diff --git a/src/main/java/com/m/linshor/controllers/LinShorController.java b/src/main/java/com/m/linshor/controllers/LinShorController.java index 3586ba5..365131f 100644 --- a/src/main/java/com/m/linshor/controllers/LinShorController.java +++ b/src/main/java/com/m/linshor/controllers/LinShorController.java @@ -53,8 +53,7 @@ public int deleteByShortUrl(@PathVariable @Parameter(description = "Short URL to @ApiResponse(responseCode = "400", description = "Invalid input") }) public ResponseEntity linshorUrl(@RequestBody @Parameter( - description = "The long URL to shorten", required = true) - String longUrl, HttpServletRequest request) { + description = "The long URL to shorten", required = true) String longUrl, HttpServletRequest request) { Mapping mapping = linShorService.saveLink(longUrl); String serverAddress = request.getRequestURL().toString().replace(request.getRequestURI(), ""); From 4210d2739ecdf792ad3d6a2bb7acaffd8ff0099b Mon Sep 17 00:00:00 2001 From: MattWay224 Date: Wed, 26 Mar 2025 01:28:28 +0300 Subject: [PATCH 3/3] reformat --- src/main/java/com/m/linshor/config/SwaggerConfig.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/m/linshor/config/SwaggerConfig.java b/src/main/java/com/m/linshor/config/SwaggerConfig.java index 27310f7..80d7279 100644 --- a/src/main/java/com/m/linshor/config/SwaggerConfig.java +++ b/src/main/java/com/m/linshor/config/SwaggerConfig.java @@ -10,8 +10,8 @@ public class SwaggerConfig { @Bean public OpenAPI customOpenAPI() { return new OpenAPI() - .info(new Info().title("LinShor URL Shortener API") - .version("1.0") - .description("API documentation for the URL shortener service")); + .info(new Info().title("LinShor URL Shortener API") + .version("1.0") + .description("API documentation for the URL shortener service")); } -} \ No newline at end of file +}