SpringBoot+HttpPatch+JsonPatch实现Json文件的更新

引言

出于对Mysql数据库减负的想法,我们决定将一些经常读的数据放在自己的json文件服务器中,当然也可以选择redis,但是可能会有较多数据不会读到但必须要存的情况比较耗内存。这里对于json文件的更新就成了一种问题,这里我们介绍下我们使用的SpringBoot+HttpPatch+JsonPatch。

HttpPatch

Http的【RFC2616】原本定义用于上传数据的方法只有POST和PUT,但是考虑到两者的不足,就增加了PATCH方法。

用PATCH方法,默认是以x-www-form-urlencoded的contentType来发送信息,并且信息内容是放在request的body里。

PUT方法和PATCH方法的提交目的地都是直接指向资源,而POST方法提交的数据的目的地是一个行为处理器。

PUT方法用来替换资源,而patch方法用来更新部分资源,然而PATCH和POST都是非幂等的,POST请求服务器执行一个动作,多次请求会多次执行。PATCH提供的实体则需要根据程序或其它协议的定义,解析后在服务器上执行,以此来修改服务器上的数据。也就是说,PATCH请求是会执行某个程序的,如果重复提交,程序可能执行多次,对服务器上的资源就可能造成额外的影响POST方法和PATCH方法它们的实体部分都是结构化的数据,所以PAtch也是非幂等的。POST方法的实体结构一般是 multipart/form-data或 application/x-www-form-urlencoded而PATCH方法的实体结构则随其它规范定义。这和PUT方法的无结构实体相比就是最大的区别。

JsonPatch

JSON Patch是一种用于描述对JSON文档所做的更改的格式(JSON Patch本身也是JSON结构)。当只更改了一部分时,可用于避免发送整个文档。可以与HTTP PATCH方法结合使用时,它允许以符合标准的方式对HTTP API进行部分更新。

JSON Patch是在IETF的RFC 6902中指定的。如果了解过linux上的diff、patch,就非常容易理解JSON Patch了,前者是针对普通文本文件的,后者是指针对JSON结构。(前者更通用)

实战

1.首先来看看需要导入哪些依赖,为了方便直接贴出全部依赖直接复制即可

     <properties>
        <!-- Source encoding -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Dependency versions -->
        <lombok.version>1.18.18</lombok.version>
        <guava.version>27.1-jre</guava.version>
        <mapstruct.version>1.3.0.Final</mapstruct.version>
        <jackson.version>2.9.8</jackson.version>
        <javax-json.version>1.1.4</javax-json.version>
        <spring-boot.version>2.1.5.RELEASE</spring-boot.version>
        <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Boot Developer Tools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Spring Boot Starter Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
        <!-- MapStruct -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <!-- JSR-353 (JSON Processing): API -->
        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>
            <version>${javax-json.version}</version>
        </dependency>
        <!-- JSR-353 (JSON Processing): Johnzon implementation -->
        <dependency>
            <groupId>org.apache.johnzon</groupId>
            <artifactId>johnzon-core</artifactId>
        </dependency>
        <!-- Jackson module for the JSR-353 (JSON Processing) -->
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr353</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!-- Apache HTTP Components -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Jayway JsonPath Assert -->
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path-assert</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Spring Boot -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Maven Compiler -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <release>11</release>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <arg>
                            -Amapstruct.suppressGeneratorTimestamp=true
                        </arg>
                        <arg>
                            -Amapstruct.suppressGeneratorVersionInfoComment=true
                        </arg>
                        <arg>
                            -Amapstruct.defaultComponentModel=spring
                        </arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.准备一个实体类和一个请求信息类

实体类

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BookInput {
    private Long id;
    private String bookName;
    private String url;
}

请求信息类

public final class PatchMediaType {

    public static final String APPLICATION_JSON_PATCH_VALUE = "application/json-patch+json";

    public static final String APPLICATION_MERGE_PATCH_VALUE = "application/merge-patch+json";

    public static final MediaType APPLICATION_JSON_PATCH;

    public static final MediaType APPLICATION_MERGE_PATCH;

    static {
        APPLICATION_JSON_PATCH = MediaType.valueOf(APPLICATION_JSON_PATCH_VALUE);
        APPLICATION_MERGE_PATCH = MediaType.valueOf(APPLICATION_MERGE_PATCH_VALUE);
    }

    private PatchMediaType() {
        throw new AssertionError("No instances of PatchMediaType for you!");
    }
}

3.写一个配置类用来对json进行配置

@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper()
                .setDefaultPropertyInclusion(Include.NON_NULL)
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .findAndRegisterModules();
    }
}

注:如果没有这个配置类会报错

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `javax.json.JsonStructure` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

4.这些准备好后重点来了

@Component
@RequiredArgsConstructor
public class PatchHelper {

    private final ObjectMapper mapper;

    private final Validator validator;

    /**
     * Performs a JSON Patch operation.
     *
     * @param patch      JSON Patch document
     * @param targetBean object that will be patched
     * @param beanClass  class of the object the will be patched
     * @param <T>
     * @return patched object
     */
    public <T> T patch(JsonPatch patch, T targetBean, Class<T> beanClass) {
        JsonStructure target = mapper.convertValue(targetBean, JsonStructure.class);
        JsonValue patched = applyPatch(patch, target);
        return convertAndValidate(patched, beanClass);
    }

    /**
     * Performs a JSON Merge Patch operation
     *
     * @param mergePatch JSON Merge Patch document
     * @param targetBean object that will be patched
     * @param beanClass  class of the object the will be patched
     * @param <T>
     * @return patched object
     */
    public <T> T mergePatch(JsonMergePatch mergePatch, T targetBean, Class<T> beanClass) {
        JsonValue target = mapper.convertValue(targetBean, JsonValue.class);
        JsonValue patched = applyMergePatch(mergePatch, target);
        return convertAndValidate(patched, beanClass);
    }

    private JsonValue applyPatch(JsonPatch patch, JsonStructure target) {
        try {
            return patch.apply(target);
        } catch (Exception e) {
            throw new UnprocessableEntityException(e);
        }
    }

    private JsonValue applyMergePatch(JsonMergePatch mergePatch, JsonValue target) {
        try {
            return mergePatch.apply(target);
        } catch (Exception e) {
            throw new UnprocessableEntityException(e);
        }
    }

    private <T> T convertAndValidate(JsonValue jsonValue, Class<T> beanClass) {
        T bean = mapper.convertValue(jsonValue, beanClass);
        validate(bean);
        return bean;
    }

    private <T> void validate(T bean) {
        Set<ConstraintViolation<T>> violations = validator.validate(bean);
        if (!violations.isEmpty()) {
            throw new ConstraintViolationException(violations);
        }
    }
}

这是封装好的JsonPatch方法直接调用就可以用了。另外两个异常处理类要放在下面

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {

}
@ResponseStatus(value = HttpStatus.UNPROCESSABLE_ENTITY)
public class UnprocessableEntityException extends RuntimeException {

    public UnprocessableEntityException(Throwable cause) {
        super(cause);
    }
    
}

5.测试

@RestController
@RequestMapping("/patch")
public class PatchController {

    private final PatchHelper patchHelper;
    public PatchController(PatchHelper patchHelper) {
        this.patchHelper = patchHelper;
    }

    @PatchMapping(consumes = PatchMediaType.APPLICATION_JSON_PATCH_VALUE)
    public void test() {
        /**
         * [{"op":"replace","path":"/id","value":3},
         * {"op":"replace","path":"/bookName","value":"HAPPYBEAR"},
         * {"op":"remove","path":"/url"},
         * {"op":"add","path":"/url","value":"https://baike.baidu.com"}]
         */
        JsonPatch patch = Json.createPatchBuilder()
                //将id替换为3
                .replace("/id",3)
                //将bookName替换为HAPPYBEAR
                .replace("/bookName", "HAPPYBEAR")
                //移除路径字段
                .remove("/url")
                //添加路径字段并赋值https://baike.baidu.com
                .add("/url", "https://baike.baidu.com")
                .build();
        //拿到的json转换为对象
        BookInput bookInput =new BookInput(2L,"大话设计模式","https://baike.baidu.com/item/%E5%A4%A7%E8%AF%9D%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/85262?fr=aladdin");
        /**
         * 开始处理
         * patch:补丁  对源json需要做的操作
         * bookInput:源json转换后的对象
         * BookInput.class:处理过后的数据转换成什么对象
         */
        BookInput input = patchHelper.patch(patch, bookInput, BookInput.class);
        System.out.println(input);
    }
}

6.运行结果

BookInput(id=3, bookName=HAPPYBEAR, url=https://baike.baidu.com)

获取或上传json文件这边就不阐述了,这边只提供SpringBoot+HttpPatch+JsonPatch的大概思路,希望对大家有帮助!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot和Netty中实现发送Json消息,你可以使用Net的`ChannelHandlerContext`对象来发送消息。下面是一个简单的示例: 首先,创建一个`JsonMessage`类表示要发送的Json消息: ```java public class JsonMessage { private String message // 省略构造方法和getter/set @Override public String toString() { return "JsonMessage{" + "message='" + message + '\'' + '}'; } } ``` 然后,在`JsonHandler`中添加逻辑以接收和发送Json消息。下面是一个示例: ```java import com.fasterxml.jackson.databind.ObjectMapper; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; public class JsonHandler extends SimpleChannelInboundHandler<String> { private ObjectMapper objectMapper = new ObjectMapper(); @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // 接收到客户端发送的Json字符串 System.out.println("Received message: " + msg); // 解析Json字符串为对象 JsonMessage jsonMessage = objectMapper.readValue(msg, JsonMessage.class); // 处理消息逻辑 // ... // 构造要发送的Json消息对象 JsonMessage response = new JsonMessage("Hello from server!"); // 将Json消息对象转换为字符串 String responseJson = objectMapper.writeValueAsString(response); // 发送Json消息给客户端 ByteBuf buf = ctx.alloc().buffer(); buf.writeBytes(responseJson.getBytes()); ctx.writeAndFlush(buf); } } ``` 在上面的示例中,我们将接收到的Json字符串解析为`JsonMessage`对象,并构造要发送的响应消息。然后,我们使用`ObjectMapper`将响应消息对象转换为Json字符串,并通过`ChannelHandlerContext`发送给客户端。 需要注意的是,你可以根据具体的业务需求在`channelRead0`方法中添加适当的处理逻辑。并且在实际应用中,你可能还需要处理异常、断开连接等其他情况。 最后,你可以通过编写Spring Boot的启动类来启动Netty服务器,并监听指定的端口: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); int port = 8080; try { new JsonServer(port).run(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们在Spring Boot应用的启动类中创建了一个新的`JsonServer`实例,并通过调用`run`方法来启动Netty服务器。 现在,你可以运行Spring Boot应用,并发送Json消息到服务器,然后服务器会将响应消息发送回客户端。记得根据自己的实际需求进行适当的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值