ShortLinkApi.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.caimei.www.controller.unlimitedApi;
  2. import com.caimei.www.service.link.ShortLinkService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.http.server.reactive.ServerHttpResponse;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import org.thymeleaf.util.StringUtils;
  10. import java.net.URI;
  11. /**
  12. * Description
  13. *
  14. * @author : plf
  15. * @date : 2021/6/24
  16. */
  17. @RestController
  18. public class ShortLinkApi {
  19. private ShortLinkService shortLinkService;
  20. @Autowired
  21. public void setShortLinkService(ShortLinkService shortLinkService) {
  22. this.shortLinkService = shortLinkService;
  23. }
  24. /**
  25. * 短链接跳转
  26. *
  27. * @param link
  28. * @param response
  29. */
  30. @GetMapping("/t/{link}")
  31. public void linkJump(@PathVariable String link, ServerHttpResponse response) {
  32. String jumpLink = shortLinkService.linkJump(link);
  33. response.setStatusCode(HttpStatus.FOUND);
  34. if (StringUtils.isEmpty(jumpLink)) {
  35. response.getHeaders().setLocation(URI.create("https://www.caimei365.com/404.html"));
  36. }
  37. response.getHeaders().setLocation(URI.create(jumpLink));
  38. }
  39. }