@Build的使用

简介
Lombok大家都知道,在使用POJO过程中,它给我们带来了很多便利,省下大量写get、set方法、构造器、equal、toString方法的时间。除此之外,通过@Builder注解,lombok还可以方便的时间建造者模式。

只需要定义一个静态公共的内部类即可。代码示例如下:

public class User {
    private Integer id;
    private String name;
    private String address;
 
    private User() {
    }
 
    private User(User origin) {
        this.id = origin.id;
        this.name = origin.name;
        this.address = origin.address;
    }
 
    public static class Builder {
        private User target;
 
        public Builder() {
            this.target = new User();
        }
 
        public Builder id(Integer id) {
            target.id = id;
            return this;
        }
 
        public Builder name(String name) {
            target.name = name;
            return this;
        }
 
        public Builder address(String address) {
            target.address = address;
            return this;
        }
 
        public User build() {
            return new User(target);
        }
    }

如果项目中有使用lombok的话,可以直接使用@Builder注解来实现

改造上面的类如下:

import lombok.Builder;
import lombok.ToString;
 
/**
  @author wulongtao
 */
@ToString
@Builder
public class UserExample {
    private Integer id;
    private String name;
    private String address;
}

如何使用:

UserExample userExample = UserExample.builder()
                .id(1)
                .name("aaa")
                .address("bbb")
                .build();
 
System.out.println(userExample);

遇到问题
在使用@Builder过程中,发现了一问题:子类的Builder对象没有父类的属性。这在使用上造成了一定的问题。
对于这个问题,找到了如下解法

对于父类,使用@AllArgsConstructor注解
对于子类,手动编写全参数构造器,内部调用父类全参数构造器,在子类全参数构造器上使用@Builder注解
通过这种方式,子类Builder对象可以使用父类的所有私有属性。
但是这种解法也有两个副作用:

因为使用@AllArgsConstructor注解,父类构造函数字段的顺序由声明字段的顺序决定,如果子类构造函数传参的时候顺序不一致,字段类型还一样的话,出了错不好发现
如果父类字段有增减,所有子类的构造器都要修改
虽然有这两个副作用,但是这种解法是我找到的唯一一种解决子类使用@Builder,能使用父类属性的方式。

参考博客评论: Lombok’s @Builder annotation and inheritance

如何在使用@Builder的模式中,加入字段的默认值。因为使用了建造者模式,那么一般在类内声明字段的时候给字段默认值的方式就是无效的,需要在建造者上动手脚。
自定义静态内部类作为建造者,赋予默认值,再使用@Builder注解,这个时候lombok会补全已有的建造者类,进而使用默认值
更新的lombok有@Builder.Default声明,注解在需要默认值的字段上即可。
在评论区也有这种方式的副作用讨论,链接是: Using Lombok’s @Builder annotation with default values

转载于:https://juejin.im/post/5cdb8422e51d453adf1fa72d

我是windows系统,用Unity打了一个Brotli包,想放进nginx里运行,但是报错,Build6.framework.js.br:1 Uncaught SyntaxError: Invalid or unexpected token (at Build6.framework.js.br:1:2)了解此错误AI Build6.loader.js:1 Unable to parse Build/Build6.framework.js.br!<br>If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported over HTTP connections. Migrate your server to use HTTPS. c @ Build6.loader.js:1 i.onload @ Build6.loader.js:1 script (匿名) @ Build6.loader.js:1 x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 Failed to parse binary data file Build/Build6.data.br (with "Content-Type: application/octet-stream"), because it is still brotli-compressed. It should have been uncompressed by the browser, but it was unable to do so since the web server provided the compressed content without specifying the HTTP Response Header "Content-Encoding: br" that would have informed the browser that decompression is needed. Please verify your web server hosting configuration to add the missing "Content-Encoding: br" HTTP Response Header. c @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 Malformed data? Downloaded binary data file Build/Build6.data.br (ArrayBuffer size: 65391783) and browser should have decompressed it, but it might have not. Dumping raw HTTP Response Headers if it might help debug: (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 accept-ranges: bytes (匿名) @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 cache-control: public, max-age=31536000, immutable (匿名) @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 connection: keep-alive (匿名) @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 content-length: 65391783 (匿名) @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 content-type: application/octet-stream (匿名) @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 date: Thu, 06 Mar 2025 03:12:06 GMT (匿名) @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 etag: "67c9123c-3e5cca7" (匿名) @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 last-modified: Thu, 06 Mar 2025 03:10:52 GMT (匿名) @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 Promise.then x @ Build6.loader.js:1 (匿名) @ Build6.loader.js:1 createUnityInstance @ Build6.loader.js:1 (匿名) @ (索引):27了解此错误AI Build6.loader.js:1 server: nginx/1.27.1
03-08
<think>嗯,用户在使用Unity打包成Brotli格式后,通过Nginx运行时遇到了错误。错误信息显示无法解析.js.br文件,提示需要检查HTTP响应头中的Content-Encoding是否设置为br。首先,我需要确认Brotli压缩在Nginx中的配置是否正确。 Brotli需要Nginx的第三方模块支持,比如ngx_brotli。用户可能没有安装或者启用这个模块。此外,Nginx的配置文件中需要针对.br文件设置正确的MIME类型,并添加Content-Encoding头。用户可能在配置中没有正确添加这些设置,导致浏览器无法识别压缩格式,从而不解压文件,直接传给Unity,引发解析错误。 另外,错误信息提到可能需要使用HTTPS,因为某些浏览器在HTTP下不支持Brotli。用户是否在使用HTTPS?如果没有,可能需要配置SSL证书或者改用Gzip压缩。 还需要检查Unity的构建设置,是否正确地生成了Brotli压缩文件,并且加载路径是否正确。可能路径配置错误,导致Nginx无法正确找到文件,或者返回了错误的MIME类型。 最后,查看Nginx的日志文件,确认是否有关于文件服务或头设置的错误信息,这有助于进一步诊断问题。可能需要逐步验证配置,比如先确保Brotli模块已安装,再检查MIME类型和响应头,最后测试HTTPS是否解决问题。</think>这个问题是由于Nginx服务器未正确配置Brotli压缩响应头导致的。以下是详细解决方案: **问题原因分析** 1. **缺少Content-Encoding头**:浏览器需要收到`Content-Encoding: br`响应头才会自动解压Brotli文件 2. **HTTP协议限制**:部分浏览器在HTTP协议下不支持Brotli解压 3. **MIME类型配置**:Nginx未正确设置`.br`文件的MIME类型 **解决方案步骤** **一、配置Nginx支持Brotli** ```nginx # 在http块中添加MIME类型 types { application/wasm wasm; application/octet-stream br; } # 在server块中添加响应头 location ~ \.br$ { add_header Content-Encoding br; add_header Content-Type application/octet-stream; gzip off; # 禁用gzip冲突 brotli off; # 如果使用预压缩文件需关闭动态压缩 } ``` **二、强制使用HTTPS(必需)** ```nginx server { listen 443 ssl; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/privkey.pem; # 其他SSL配置... } ``` **三、Unity构建配置验证** 1. 在Player Settings > Publishing Settings: - 启用`Compression Format`为Brotli - 确保`Decompression Fallback`已禁用 **四、文件部署检查** 确保部署结构包含: ``` ├── Build │ ├── Build6.framework.js.br │ ├── Build6.data.br │ └── ... └── index.html ``` **五、完整Nginx配置示例** ```nginx http { # 安装ngx_brotli模块后启用 brotli on; brotli_static on; brotli_types application/wasm application/javascript application/octet-stream; server { listen 443 ssl; server_name yourdomain.com; ssl_certificate /etc/ssl/certs/your_domain.crt; ssl_certificate_key /etc/ssl/private/your_domain.key; location / { root /path/to/unity/build; try_files $uri $uri.br @nobrotli; # 预压缩文件处理 location ~ \.br$ { add_header Content-Encoding br; add_header Content-Type application/octet-stream; brotli off; } } location @nobrotli { # 备用处理(不建议) } } } ``` **测试验证方法** 1. 使用curl检查响应头: ```bash curl -I -H 'Accept-Encoding: br' https://yourdomain.com/Build/Build6.framework.js.br ``` 应返回: ``` HTTP/1.1 200 OK Content-Encoding: br Content-Type: application/octet-stream ``` **常见问题排查** 1. 确认Nginx编译时包含`--add-module=/path/to/ngx_brotli` 2. 检查文件权限:`chmod -R 755 /path/to/build` 3. 清除浏览器缓存强制刷新 4. 使用Chrome开发者工具 > Network标签验证响应头 **备选方案** 如果仍存在问题,可以临时改用Gzip压缩: 1. Unity构建时选择Gzip格式 2. 修改Nginx配置: ```nginx location ~ \.gz$ { add_header Content-Encoding gzip; gzip off; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值