网站建设有什么要求,如何提高网站响应速度,网站模块标准版,最近免费高清观看mvjava spr显然#xff0c;编写URL缩短服务是新的“ Hello#xff0c;world#xff01; ”在IoT /微服务/时代的世界中。 一切始于在45行Scala中的URL缩短服务 -整洁的Scala#xff0c;以Spray和Redis进行调味以进行存储。 紧随其后的是#xff0c; 在35行Clojure中提供了ur… java spr 显然编写URL缩短服务是新的“ Helloworld ”在IoT /微服务/时代的世界中。 一切始于在45行Scala中的URL缩短服务 -整洁的Scala以Spray和Redis进行调味以进行存储。 紧随其后的是 在35行Clojure中提供了url缩短服务 甚至在Haskell的43行中提供了URL缩短服务 。 所以我内心的反时髦人士问用Java语言要花多长时间 但是出于善意不是普通的Java。 带有Spring Data Redis的 Spring Boot是一个很好的起点。 我们需要的只是一个处理GET和POST的简单控制器 import com.google.common.hash.Hashing;
import org.apache.commons.validator.routines.UrlValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.*;
import java.nio.charset.StandardCharsets;org.springframework.boot.autoconfigure.EnableAutoConfiguration
org.springframework.stereotype.Controller
public class UrlShortener {public static void main(String[] args) {SpringApplication.run(UrlShortener.class, args);}Autowired private StringRedisTemplate redis;RequestMapping(value /{id}, method RequestMethod.GET)public void redirect(PathVariable String id, HttpServletResponse resp) throws Exception {final String url redis.opsForValue().get(id);if (url ! null)resp.sendRedirect(url);elseresp.sendError(HttpServletResponse.SC_NOT_FOUND);}RequestMapping(method RequestMethod.POST)public ResponseEntityString save(HttpServletRequest req) {final String queryParams (req.getQueryString() ! null) ? ? req.getQueryString() : ;final String url (req.getRequestURI() queryParams).substring(1);final UrlValidator urlValidator new UrlValidator(new String[]{http, https});if (urlValidator.isValid(url)) {final String id Hashing.murmur3_32().hashString(url, StandardCharsets.UTF_8).toString();redis.opsForValue().set(id, url);return new ResponseEntity(http://mydomain.com/ id, HttpStatus.OK);} elsereturn new ResponseEntity(HttpStatus.BAD_REQUEST);}
} 该代码很好地自我描述并且在功能上等同于Scala中的版本。 我没有尝试过太多的压缩以使行数尽可能的短上面的代码很典型只有很少的细节 我通常不使用通配符导入 我不使用完全限定的类名我承认我想保存一个import行 我用if括号包围if else用括号括起来 我几乎从不使用场注入这是控制家族反转中最丑陋的兄弟。 相反我会去让构造函数允许使用模拟的Redis进行测试 Autowired
private final StringRedisTemplate redis;public UrlShortener(StringRedisTemplate redis) {this.redis redis;
} 我最苦恼的事情是……获取原始的完整URL。 基本上我需要.com或port之后的所有内容。 没有流血的方式既没有servlet也没有Spring MVC因此笨拙的getQueryString()摆弄着。 您可以按以下方式使用该服务-创建较短的URL $ curl -vX POST localhost:8080/https://www.google.pl/search?qtomasznurkiewicz POST /https://www.google.pl/search?qtomasznurkiewicz HTTP/1.1User-Agent: curl/7.30.0Host: localhost:8080Accept: */*HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Type: text/plain;charsetISO-8859-1Content-Length: 28Date: Sat, 23 Aug 2014 20:47:40 GMThttp://mydomain.com/50784f51 通过较短的URL重定向 $ curl -v localhost:8080/50784f51 GET /50784f51 HTTP/1.1User-Agent: curl/7.30.0Host: localhost:8080Accept: */*HTTP/1.1 302 FoundServer: Apache-Coyote/1.1Location: https://www.google.pl/search?qtomasznurkiewiczContent-Length: 0Date: Sat, 23 Aug 2014 20:48:00 GMT为了完整起见这是Gradle中的一个构建文件maven也可以使用在所有以前的解决方案中都跳过了 buildscript {repositories {mavenLocal()maven { url http://repo.spring.io/libs-snapshot }mavenCentral()}dependencies {classpath org.springframework.boot:spring-boot-gradle-plugin:1.1.5.RELEASE}
}apply plugin: java
apply plugin: spring-bootsourceCompatibility 1.8repositories {mavenLocal()maven { url http://repository.codehaus.org }maven { url http://repo.spring.io/milestone }mavenCentral()
}dependencies {compile org.springframework.boot:spring-boot-starter-web:1.1.5.RELEASEcompile org.springframework.boot:spring-boot-starter-redis:1.1.5.RELEASEcompile com.google.guava:guava:17.0compile org.apache.commons:commons-lang3:3.3.2compile commons-validator:commons-validator:1.4.0compile org.apache.tomcat.embed:tomcat-embed-el:8.0.9compile org.aspectj:aspectjrt:1.8.1runtime cglib:cglib-nodep:3.1
}tasks.withType(GroovyCompile) {groovyOptions.optimizationOptions.indy true
}task wrapper(type: Wrapper) {gradleVersion 2.0
} 实际上也是42行...这就是整个应用程序没有XML没有描述符没有安装。 对于最短最模糊的工作代码我不认为此练习只是一个虚拟的代码。 带有Redis后端的URL缩短器Web服务是给定语言和生态系统的语法和功能的有趣展示。 有趣的是还有很多算法问题例如Rosetta代码中发现的问题。 这也是编写REST服务的一个很好的最低限度模板。 原始Scala实现的一个重要功能包括该实现在所有实现中都以某种方式被默默地忘记了它是非阻塞的。 HTTP和Redis的访问是事件驱动的 React 没事我说所以我想它可以同时处理客户数以万计。 阻止由Tomcat支持的控制器无法实现这一点。 但是您仍然必须承认这种用Java编写的服务甚至不是Java 8简明扼要易于遵循和简单明了-其他解决方案都不是可读的这当然是主观的。 等待别人 翻译自: https://www.javacodegeeks.com/2014/08/url-shortener-service-in-42-lines-of-code-in-java-spring-boot-redis.htmljava spr