小编Joã*_*aro的帖子

使用ByteBuddy重新定义java.lang类

我正在尝试使用ByteBuddy重新定义java.lang包(如String.class或Integer.class)上的类,但没有成功.我的问题是,如果可能的话?

这是我在java代理中尝试的代码:

public static void premain(String agentArgs, Instrumentation inst) {
    new AgentBuilder.Default()
            .type(named("java.lang.String"))
            .transform((builder, typeDescription, classLoader) ->
                    builder.method(named("toString"))
                            .intercept(FixedValue.value("toString() got hacked!")))
            .with(AgentBuilder.Listener.StreamWriting.toSystemOut())
            .with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
            .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
            .installOn(inst);
}
Run Code Online (Sandbox Code Playgroud)

当我检查日志的输出时,我看到的有关String类的信息是:

[Byte Buddy] IGNORE [[Ljava.lang.String; [null, null]
[Byte Buddy] COMPLETE [[Ljava.lang.String; [null, null]
Run Code Online (Sandbox Code Playgroud)

这是否意味着ByteBuddy没有重新定义String类?这甚至可能吗?

非常感谢.

java byte-buddy

2
推荐指数
1
解决办法
663
查看次数

将嵌套的 json 映射到具有原始 json 值的 pojo

我有一个嵌套的 json pojo,其中 json 的嵌套部分用@JsonRawValue. 我正在尝试使用 rest 模板进行映射,但出现错误 JSON 解析错误:Cannot deserialize instance of java.lang.String out of START_OBJECT token;

嵌套异常是com.fasterxml.jackson.databind.exc.MismatchedInputException.

这是我的响应对象的样子:

import com.fasterxml.jackson.annotation.JsonRawValue;

public class ResponseDTO {

    private String Id;
    private String text;
    @JsonRawValue
    private String explanation;

    //getters and setters;

}
Run Code Online (Sandbox Code Playgroud)

explanation映射到字符串的 json在哪里。这适用于邮递员,招摇,我在响应中看到解释为 json。

但是当我使用 Rest Template 测试它时:

    ResponseEntity<ResponseDTO> resonseEntity = restTemplate.exchange(URI, HttpMethod.POST, requestEntity, ResponseDTO.class);
Run Code Online (Sandbox Code Playgroud)

我看到这个例外:

org.springframework.web.client.RestClientException: Error while extracting 
response for type [class com.**.ResponseDTO] and content type 
[application/json;charset=utf-8]; nested exception is 
org.springframework.http.converter.HttpMessageNotReadableException: JSON 
parse …
Run Code Online (Sandbox Code Playgroud)

java rest spring json jackson

2
推荐指数
1
解决办法
1551
查看次数

标签 统计

java ×2

byte-buddy ×1

jackson ×1

json ×1

rest ×1

spring ×1