小编and*_*ero的帖子

如何从观察者文件中排除 node_modules

当我这样做时ng serve,日志会显示如下错误:

Error from chokidar (/PATH_OF_MY_PROJECT/node_modules/ionicons/dist/ionicons/svg): Error: ENOSPC: System limit for number of file watchers reached, watch '/PATH_OF_MY_PROJECT/node_modules/ionicons/dist/ionicons/svg/ios-subway.svg'
Run Code Online (Sandbox Code Playgroud)

我读到解决方案是在 Linux 上更改观察者文件,但这是一个糟糕的解决方案!

为什么 Angular 需要看到对 的更改node_modules?这是一个不会改变的文件夹。如果我需要安装一个新库,我可以重新启动服务器。

如果您的解决方案是:请不要回答我 echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf。我不想使用这个糟糕的解决方案。

watch angular-cli chokidar angular-devkit

6
推荐指数
1
解决办法
1468
查看次数

为什么时区模式"OOOO"没有显示完整的GMT + 00:00偏移格式?

这是一个错误还是一个功能?

DateTimeFormatterJavaDoc中明确规定,当我用OOOO我的格式化模式,完整形式的本地化时区时,应采用(重点煤矿):

四个字母输出完整形式,这是本地化的偏移文本,例如'GMT,具有2位小时和分钟字段,可选的第二字段(如果非零)和冒号,例如'GMT + 08:00'.

但如果时间是GMT + 0:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE yyyy.MM.dd HH:mm:ss.SSS OOOO");

String timestamp = OffsetDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).format(formatter);
System.out.println(timestamp);
Run Code Online (Sandbox Code Playgroud)

这是输出:

Mon 2019.02.25 22:30:00.586 GMT
Run Code Online (Sandbox Code Playgroud)

预期:

Mon 2019.02.25 22:30:00.586 GMT+00:00
Run Code Online (Sandbox Code Playgroud)

java datetime datetime-format timezone-offset java.time

5
推荐指数
1
解决办法
107
查看次数

正确使用Spring Boot的ErrorController和Spring的ResponseEntityExceptionHandler

问题

在Spring Boot中创建控制器以自定义方式处理所有错误/异常(包括自定义异常)时,应首选哪种技术?

  1. 控制器是否应该实现Spring Boot的ErrorController

  2. 控制器是否应该扩展Spring的ResponseEntityExceptionHandler

  3. 两者:实现和扩展两个类(包括它们的功能)的单个控制器?

  4. 两者:两个单独的控制器,一个执行ErrorController,另一个扩展ResponseEntityExceptionHandler

目标

这篇文章的原因是在Spring Boot中找到一种具有以下所有属性的异常处理方法:

  • Throwable在处理请求期间,所有出现在控制器/过滤器/拦截器中的信号都应被捕获。
  • 在受凉的情况下Throwable,我们希望暴露任何堆栈跟踪或其他实施细则,以客户永远(除非明确编码的方式)。
  • 应该有可能Throwable按其类分别处理所有发生的。对于任何其他未指定的类型,可以指定默认响应。(我肯定知道,这可以实现的@ExceptionHandler。但是ErrorController?)
  • 该代码应尽可能简洁明了,没有丑陋的解决方法或UB来实现此目标。

更多细节

我注意到,两个控制器(请参见上面的1和2)都可能包含返回ResponseEntity对象的方法,从而处理发生的异常并将响应返回给客户端。因此,它们在理论上可以产生相同的结果?

那里有关于如何使用技术1和2的一些教程。但是,我发现没有文章同时考虑这两种选择,将它们进行比较或一起使用,这引发了一些其他问题:

  1. 他们甚至应该一起考虑吗?

  2. 这两种提议的技术之间的主要区别是什么?有何相似之处?

  3. 一个是另一个的更强大的版本吗?有什么可以做的而另一个不能做的,反之亦然?

  4. 它们可以一起使用吗?在某些情况下有必要这样做吗?

  5. 如果将它们一起使用,将如何处理异常?它是通过两个处理程序还是通过一个处理程序?在后者的情况下,哪个?

  6. 如果将它们一起使用,并且在异常处理期间在控制器内部(一个或另一个)抛出异常,该异常将如何处理?是否发送到另一个控制器?从理论上讲,异常可以在控制器之间开始反弹还是创建其他类型的不可恢复循环吗?

  7. 关于Spring Boot在ResponseEntityExceptionHandler内部如何使用Spring 或如何期望它在Spring Boot应用程序中使用,是否有任何可信/官方文档?

  8. 如果ResponseEntityExceptionHandler一个人已经足够了,那为什么ErrorController存在呢?

ResponseEntityExceptionHandler@ExceptionHandler注解一起查看Spring时,它似乎在分别处理不同类型的异常和使用更简洁的代码方面更强大。但是由于Spring Boot是基于Spring构建的,因此这意味着:

  • 当使用Spring Boot时,我们应该实现ErrorController …

java error-handling spring exception spring-boot

4
推荐指数
1
解决办法
1232
查看次数

我们什么时候需要在 Spring 中 .save() 一个实体?

在一个相对较大的 Spring Boot 项目中,我有一个具有以下(过于简化的)事件序列的方法:

Car car = carRepository.save(new Car());
Person person = personRepository.save(new Person(car)); // Car is a field of Person

Engine engine = engineRepository.save(new Engine());
person.getCar().setEngine(engine);

carRepository.save(person.getCar()); // without this the engine and car relation is not registered
Run Code Online (Sandbox Code Playgroud)

CarPersonEngine都是@Entity类(数据库对象)。对于这个例子,它们的实现可能如下:

// Car.java
@Entity
@Table(name = "car_tbl")
public class Car {
    @Id
    @GeneratedValue
    @Column(name = "car_id")
    private Long id;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "engine_id", unique = true)
    private Engine engine;
}

// Person.java …
Run Code Online (Sandbox Code Playgroud)

hibernate open-session-in-view spring-data-jpa spring-boot

4
推荐指数
1
解决办法
4576
查看次数

如何使新的行命令在从Internet打开的.txt文件中工作?

我刚刚开始使用Python,我正在尝试制作一个程序,在互联网"www ....../lyrics.txt"打开的屏幕上写一首歌的歌词.我的第一个代码:

    import urllib.request
    lyrics=urllib.request.urlopen("http://hereIsMyUrl/lyrics.txt")
    text=lyrics.read()
    print(text)
Run Code Online (Sandbox Code Playgroud)

当我激活这段代码时,它没有给我在网站上写的歌词,它给了我新的行命令'\ r \n'在应该是新行的所有地方并给了我所有的歌词在一个长杂乱的字符串.例如:这里的一些歌词\ r \n这应该已经是下一行了\ r \n等等.

我在互联网上搜索代码,用新行代替'\ r \n'命令并尝试以下方法:

    import urllib.request
    lyrics=urllib.request.urlopen("http://hereIsMyUrl/lyrics.txt")
    text=lyrics.read()
    text=text.replace("\r\n","\n")
    print(text)
Run Code Online (Sandbox Code Playgroud)

我希望它至少可以替换一些东西,但它给了我一个运行时错误:

    TypeError: expected bytes, bytearray or buffer compatible object
Run Code Online (Sandbox Code Playgroud)

我在互联网上搜索了这个错误,但我没有找到任何与从互联网上打开文件有关的内容.

我已经被困在这一点上几个小时,不知道如何继续.请帮忙!提前致谢!

python replace runtime-error

3
推荐指数
1
解决办法
4604
查看次数

为什么不向java.time.Instant添加周数?

以下代码:

Instant inFourWeeks = Instant.now().plus(4L, ChronoUnit.WEEKS);
Run Code Online (Sandbox Code Playgroud)

引发异常:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Weeks
Run Code Online (Sandbox Code Playgroud)

为什么几周都不受支持?我理解为什么不支持数月和数年,因为它们在较小单位的持续时间可能会有所不同.但是一周有不变的持续时间(7天),我可以通过写作来实现同样的目标:

Instant inFourWeeks = Instant.now().plus(4L * 7L, ChronoUnit.DAYS);
Run Code Online (Sandbox Code Playgroud)

java time calendar java.time.instant java.time

3
推荐指数
2
解决办法
717
查看次数

何时使用 Spring 的 @Repository 注解?

在阅读有关在 Spring 中创建自定义查询的信息时,我注意到没有任何类 ( UserRepositoryCustom, UserRepositoryCustomImpl, UserRepository) 使用该@Repository注释。

我对此感到惊讶,因为通常所有 Spring 应用程序类都用一些东西进行注释,以便检测它们并为它们生成代理。这就是面向方面的框架在 Java 中的工作方式。

然后我检查了我的旧工作代码,发现我的存储库界面也没有使用注释。然而,它是@Autowired对使用它的控制器和其他类的依赖。例子:

// AddressRepository.java
public interface AddressRepository extends CrudRepository<Address, String> {
}

// Repositories.java
@Getter // lombok
@Component
public class Repositories { // autowire this to have access to all repo's
    private final AddressRepository addressRepository;

    @Autowired
    public Repositories(AddressRepository addressRepository) {
        this.addressRepository = addressRepository;
    }
}
Run Code Online (Sandbox Code Playgroud)

这就引出了一个问题:什么时候应该使用注释?以及Spring 的组件扫描如何自动检测到未注释的类?

autowired spring-annotations spring-repositories

3
推荐指数
2
解决办法
3999
查看次数

@EnableWebMvc annotation changes response encoding

By default, my Spring Boot application responds to any request with the following Content-Type headers:

Content-Type: text/html;charset=UTF-8
Run Code Online (Sandbox Code Playgroud)

I believe this has been so from the beginning and I would like to keep it this way.

However, by making a simple change and only adding @EnableWebMvc to my @SpringBootApplication annotated class, the charset attribute is changed:

Content-Type: text/html;charset=ISO-8859-1
Run Code Online (Sandbox Code Playgroud)

And so the UTF-8 encoded content of the response is displayed incorrectly on the website.

spring spring-mvc character-encoding http-headers spring-boot

0
推荐指数
1
解决办法
64
查看次数