在gs-uploaded-files Spring Boot示例中将Java 1.8 lambdas转换为Java 1.7
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在gs-uploaded-files Spring Boot示例中将Java 1.8 lambdas转换为Java 1.7,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3708字,纯文字阅读大概需要6分钟。
内容图文
我在https://spring.io/ Spring Boot上进行了一些练习.
做一个例子https://spring.io/guides/gs/uploading-files/,当我使用Java 8时工作正常,但遗憾的是我在Web服务中包装的代码需要Java 7.我已经列出了所有的错误代码,有人可以帮我将lambda转换为符合1.7的代码并替换新库(java.util.stream.Stream和java.util.stream.Collectors).
Application.java
@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner init(StorageService storageService) {
return (args) -> {
storageService.deleteAll();
storageService.init();
};
}
}
return (args) -> errors out saying “use source -8 or higher to enable
lambda expressions”
FileUploadController.java
import java.util.stream.Collectors
//..
@GetMapping("/")
public String listUploadedFiles(Model model) throws IOException {
model.addAttribute("files", storageService
.loadAll()
.map(path ->
MvcUriComponentsBuilder
.fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
.build().toString())
.collect(Collectors.toList()));
return "uploadForm";
}
package java.util.stream does not exist
type loadAll() is erroneous
errors out saying “use source -8 or higher to enable
lambda expressions”
FileSystemStorageService.java
@Override
public Stream<Path> loadAll() {
try {
return Files.walk(this.rootLocation, 1)
.filter(path -> !path.equals(this.rootLocation))
.map(path -> this.rootLocation.relativize(path));
} catch (IOException e) {
throw new StorageException("Failed to read stored files", e);
}
}
cannot find symbol walk
errors out saying “use source -8 or higher to enable
lambda expressions”
StorageService.java
import java.util.stream.Stream;
public interface StorageService {
void init();
void store(MultipartFile file);
Stream<Path> loadAll();
Path load(String filename);
Resource loadAsResource(String filename);
void deleteAll();
}
package java.util.stream does not exist
解决方法:
斗牛,我很抱歉你的赏金,但你真的只有很少的选择. Stream API仅在Java 8中添加,因此在Java 7中不存在.您可以通过手动编写匿名类甚至几乎自动来解决与lambda相关的问题(参见Retrolambda).但是使用Stream API,您只有两个选择:
>手动将Backport Stream API转换为Java 7.或者使用其他人尝试在SourceForge上反向移植,例如streamsupport project(或在GitHub上复制)
>摆脱Stream API并使用较旧的Java 7类.
更新(替换Files.walk)
如果您的Files.walk是唯一使用Java-8特定API的地方,您可以相对轻松地将其替换为Java 7 API:
interface PathNameMapper
{
String mapPath(Path path);
}
List<String> loadAll(PathNameMapper mapper)
{
try
{
List<String> result = new ArrayList<>();
Files.walkFileTree(rootLocation, EnumSet.noneOf(FileVisitOption.class), 1, new SimpleFileVisitor<Path>()
{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
if (!rootLocation.equals(file))
{
result.add(mapper.mapPath(rootLocation.relativize(file)));
}
return FileVisitResult.CONTINUE;
}
});
return result;
}
catch (IOException e)
{
throw new StorageException("Failed to read stored files", e);
}
}
然后你的listUploadedFiles会变成类似的东西
@GetMapping ("/")
public String listUploadedFiles(Model model) throws IOException
{
model.addAttribute("files", storageService
.loadAll(new PathNameMapper()
{
@Override
public String mapPath(Path path)
{
return MvcUriComponentsBuilder
.fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
.build().toString();
}
}));
return "uploadForm";
}
更新2(labmda – >匿名类转换)
仅仅为了完整性,一个如何手动将lambda转换为匿名类的示例:
@Bean
CommandLineRunner init(StorageService storageService)
{
return new CommandLineRunner()
{
@Override
public void run(String... args) throws Exception
{
storageService.deleteAll();
storageService.init();
}
};
}
内容总结
以上是互联网集市为您收集整理的在gs-uploaded-files Spring Boot示例中将Java 1.8 lambdas转换为Java 1.7全部内容,希望文章能够帮你解决在gs-uploaded-files Spring Boot示例中将Java 1.8 lambdas转换为Java 1.7所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。