我试图访问一个应用了一些过滤器的spring Boot 应用程序的端点,但过滤器无法正常工作。我需要一些关于这个问题的帮助,我在下面提供了可疑的东西。
package com.ronit.Restfulwebservice.filtering;
import java.util.Arrays;
import java.util.List;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
@RestController
public class FilteringController {
@GetMapping("/filtering")
public MappingJacksonValue filtering() {
SomeBean someBean = new SomeBean("value1", "value2", "value3");
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(someBean);
SimpleBeanPropertyFilter filter =
SimpleBeanPropertyFilter.filterOutAllExcept("field1","field3");
FilterProvider filters =
new SimpleFilterProvider().addFilter("SomeBeanFilter", filter);
mappingJacksonValue.setFilters(filters);
return mappingJacksonValue;
}
@GetMapping("/filtering-list")
public MappingJacksonValue filteringList() {
List<SomeBean> list = Arrays.asList(new SomeBean("value1", "value2", "value3"),
new SomeBean("value4", "value5", "value6"));
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(list);
SimpleBeanPropertyFilter filter =
SimpleBeanPropertyFilter.filterOutAllExcept("field2");
FilterProvider filters =
new SimpleFilterProvider().addFilter("SomeBeanFilter", filter);
mappingJacksonValue.setFilters(filters);
return mappingJacksonValue;
}
}
字符串
这是SomeBean类
package com.ronit.Restfulwebservice.filtering;
import com.fasterxml.jackson.annotation.JsonFilter;
//@JsonIgnoreProperties({"field1"})
public class SomeBean {
private String field1;
//@JsonIgnore
@JsonFilter("SomeBeanFilter")
//@JsonFilter("ListFilter")
private String field2;
private String field3;
public SomeBean(String field1, String field2, String field3) {
super();
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
public String getField1() {
return field1;
}
public String getField2() {
return field2;
}
public String getField3() {
return field3;
}
@Override
public String toString() {
return "SomeBean [field1=" + field1 + ", field2=" + field2 + ", field3=" + field3 + "]";
}
}
型
这是Application类
package com.ronit.Restfulwebservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestfulWebServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RestfulWebServiceApplication.class, args);
}
}
型
application.properties文件
logging.level.org.springframework=debug
型
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.ronit</groupId>
<artifactId>Restful-web-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Restful-web-service</name>
<description>My first rest api </description>
<properties>
<java.version>20</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<!--<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>-->
<!-- This dependency below converts json file to xml format without this dependency
xml instead of json is given to the browser and the browser displays the same -->
<!--<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
型
http://localhost:8080/filtering-list端点的JSON响应
[
{
"field1": "value1",
"field2": "value2",
"field3": "value3"
},
{
"field1": "value4",
"field2": "value5",
"field3": "value6"
}
]
型
http://localhost:8080/filtering端点的JSON响应
{
"field1": "value1",
"field2": "value2",
"field3": "value3"
}
型
我正在学习udemy的课程,我在教程中做了完全相同的事情,并按照github上的一步一步的指南构建了完全相同的东西,但它不起作用。我已经在网上搜索了解决方案,但我没有找到,请帮助我解决这个问题。如果需要任何更多的细节,请提前询问谢谢
2条答案
按热度按时间flseospp1#
根据提供的代码,您似乎正在使用Jackson的MappingJacksonValue和SimpleBeanPropertyFilter对JSON响应应用动态过滤。代码看起来正确,端点 */filtering-list * 的响应看起来正常。但是,该问题可能与过滤器的配置或使用有关。
下面,我建议检查一些要点:
**1.**验证ObjectMapper bean是否在应用中正确配置。确保向ObjectMapper bean注册FilterProvider,以便它识别过滤器。
**2.**确保SomeBean类使用@JsonFilter(“SomeBeanFilter”)进行注解,您已经正确使用了该注解。
**3.**检查可能影响JSON响应的任何其他过滤器或拦截器配置。
**4.**确保您访问的是端点 */过滤 * 的正确URL。
配置bean的示例:
字符串
如果您已经尝试了上述步骤,但问题仍然存在,我建议您检查其他自定义配置、过滤器或拦截器是否可能干扰过滤过程。此外,在访问 */过滤 * 端点时,请检查是否有任何错误日志或异常。
ndh0cuux2#
我发现了我的错误,我把@JsonFilter(“someBeanFilter”)注解放在了String属性field 2上,而不是类上。我把它放在课堂上,现在一切正常。