gpt4 book ai didi

java - 无法使用 @Proxy 和 Jackson 创建 REST 响应

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:17 25 4
gpt4 key购买 nike

我的 LoginResp 对象的 Jackson 序列化遇到了问题。它向我展示了

Infinite recursion (StackOverflowError) (through reference chain: java.util.logging.LogManager["systemContext"]->java.util.logging.SystemLoggerContext["namedLoggers"]->java.util.Hashtable["global"]->java.util.logging.LoggerWeakRef["referent"]->java.util.logging.Logger["manager"]->java.util.logging.LogManager["systemContext"]->java.util.logging.SystemLoggerContext["namedLoggers"]->java.util.Hashtable["global"]->java.util.logging.LoggerWeakRef["referent"]->java.util.logging.Logger["manager"]->java.util.logging.LogManager["systemContext"]->java.util.logging.SystemLoggerContext["namedLoggers"]->java.util.Hashtable["global"]->java.util.logging.LoggerWeakRef["referent"]-.....

我正在尝试复制相同的场景并使用相同的代码,如 this link 中所述。 。但出现上述错误。请帮忙,因为我只是想将“resp”bean 的值写入“someJsonString”变量。提前致谢。

这些是我的依赖项

<properties>
<servlet-api-version>3.1.0</servlet-api-version>
<spring-webmvc-version>4.2.4.RELEASE</spring-webmvc-version>
<jackson-version>2.6.4</jackson-version>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-webmvc-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

我的LoginResp类

package my.beans.resp;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

@Component
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class LoginResp {

private String username;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

}

我的 REST Controller

package my.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

import my.beans.req.LoginReqB;
import my.beans.resp.LoginResp;

@RestController
@RequestMapping(path = "/login")
public class LoginC {

@Autowired
private LoginResp resp;

@RequestMapping(method = RequestMethod.POST)
public String authenticateUserLogin(@RequestBody LoginReqB request) {

resp.setUsername("abc");

ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
SimpleFilterProvider filterP = new SimpleFilterProvider().addFilter("loginResp",
SimpleBeanPropertyFilter.filterOutAllExcept("username"));
mapper.setFilterProvider(filterP);

String someJsonString = "";
try {
someJsonString = mapper.writeValueAsString(resp);
} catch (JsonProcessingException e) {
System.out.println(e.getMessage());
}
return someJsonString;
}

}

最佳答案

我解决了。

观察:Jackson 无法在线转换/处理代理对象

someJsonString = mapper.writeValueAsString(resp); 在 try block 中。

解决方案:代理对象包裹在实际对象周围。我正在使用提到的函数 here 访问代理后面的实际对象.

效果:现在,不需要自定义我的ObjectMapper,即

ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
SimpleFilterProvider filterP = new SimpleFilterProvider().addFilter("loginResp",
SimpleBeanPropertyFilter.filterOutAllExcept("username"));
mapper.setFilterProvider(filterP);

不再需要。

关于java - 无法使用 @Proxy 和 Jackson 创建 REST 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35538696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com