如何使@JsonView注释的restapi序列化所有字段
我有一个RESTAPI,比如“/users/{userId}” 此api返回用户数据,但通过@JsonView(ResourceView.Public.class)注释过滤掉密码 但我想在单元测试运行时获取密码。 在测试运行时,是否有方法进行igore@JsonView注释。 我还有其他选择吗如何使@JsonView注释的restapi序列化所有字段,json,spring,testing,jackson,Json,Spring,Testing,Jackson,我有一个RESTAPI,比如“/users/{userId}” 此api返回用户数据,但通过@JsonView(ResourceView.Public.class)注释过滤掉密码 但我想在单元测试运行时获取密码。 在测试运行时,是否有方法进行igore@JsonView注释。 我还有其他选择吗 public class ResourceView { public interface Public {} public interface Friends extends Public
public class ResourceView {
public interface Public {}
public interface Friends extends Public {}
public interface Family extends Friends {}
}
public class User {
@JsonView(ResourceView.Public.class)
private String name;
@JsonView(ResourceView.Family.class)
private String password;
}
@RestController
public class UserController {
@Autowired
private UserService userService;
@JsonView(ResourceView.Public.class)
@GetMapping(value = "/users/{userId}")
public User getUser(@PathVariable("userId") String userId) {
return userService.getUser(userId);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = "test")
public class UserServiceTest {
@Autowired
protected TestRestTemplate restTemplate;
@Value("${local.server.port}")
private int port;
protected String apiEndpoint;
@Before
protected void setUp() {
initRequestContext();
apiEndpoint = "http://localhost:" + port;
}
protected ResponseEntity<User> requestGetUser(String userId) {
ResponseEntity<User> res = restTemplate.exchange(
apiEndpoint + "/users/" + userId,
HttpMethod.GET,
new HttpEntity<>("parameters", createDefaultHttpHeaders()),
new ParameterizedTypeReference<User>() {});
return res;
}
@Test
public void testGetUser() throws Exception {
ResponseEntity<User> apiRes = requestGetUsers(request);
assertThat(apiRes.getStatusCode(), is(HttpStatus.OK));
User user = apiRes.getBody();
assertThat(user.getName(), is(notNullValue()));
assertThat(user.getPassword(), is(notNullValue()));
}
}
@Configuration
public class MyConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper().configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
return objectMapper;
}
}
公共类资源视图{
公共接口公共{}
公共接口朋友扩展公共{}
公共接口家庭扩展朋友{}
}
公共类用户{
@JsonView(ResourceView.Public.class)
私有字符串名称;
@JsonView(ResourceView.Family.class)
私有字符串密码;
}
@RestController
公共类用户控制器{
@自动连线
私人用户服务;
@JsonView(ResourceView.Public.class)
@GetMapping(value=“/users/{userId}”)
公共用户getUser(@PathVariable(“userId”)字符串userId){
返回userService.getUser(userId);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class,webEnvironment=webEnvironment.RANDOM\u端口)
@ActiveProfiles(profiles=“test”)
公共类UserServiceTest{
@自动连线
受保护的TestRestTemplate restTemplate;
@值(${local.server.port}”)
专用int端口;
受保护的字符串端点;
@以前
受保护的无效设置(){
initRequestContext();
apiEndpoint=”http://localhost:“+港口;
}
受保护的ResponseEntity requestGetUser(字符串用户ID){
ResponseEntity res=restemplate.exchange(
apiEndpoint+“/users/”+userId,
HttpMethod.GET,
新建HttpEntity(“参数”,createDefaultHttpHeaders()),
新的ParameteredTypeReference(){});
返回res;
}
@试验
public void testGetUser()引发异常{
ResponseEntity apiRes=requestGetUsers(请求);
断言(apiRes.getStatusCode(),是(HttpStatus.OK));
User=apiRes.getBody();
断言(user.getName(),是(notNullValue());
断言(user.getPassword(),是(notNullValue());
}
}
@配置
公共类MyConfig{
@豆子
公共对象映射器对象映射器(){
ObjectMapper ObjectMapper=new ObjectMapper().configure(MapperFeature.DEFAULT\u VIEW\u INCLUSION,true);
返回对象映射器;
}
}
您是否尝试过下面提到的方法:您呢,jackson?有什么想法吗?我将ObjectMapper的默认_VIEW_INCLUSION属性设置为true。但是,如果我从填充的密码中删除@JsonView注释,那么所有api用户都可以看到密码。那不是我想要的。你试过下面提到的东西吗:你呢,杰克逊?有什么想法吗?我将ObjectMapper的默认_VIEW_INCLUSION属性设置为true。但是,如果我从填充的密码中删除@JsonView注释,那么所有api用户都可以看到密码。那不是我想要的。