在Spring JSON查询中,如果不想发送@IdClass对象,可以通过以下方法实现:
@IdClass
public class MyClass {
@JsonIgnore
private Long id;
// other properties and methods
}
@IdClass
public class MyClass {
private Long id;
private String name;
// other properties and methods
}
public class MyDto {
private String name;
// other properties and methods
}
@RestController
public class MyController {
@Autowired
private MyService myService;
@GetMapping("/my-endpoint")
public MyDto getMyDto() {
MyClass myClass = myService.getMyClass();
MyDto myDto = new MyDto();
myDto.setName(myClass.getName());
// set other properties
return myDto;
}
}
@IdClass
@JsonSerialize(using = MySerializer.class)
public class MyClass {
private Long id;
private String name;
// other properties and methods
}
public class MySerializer extends JsonSerializer<MyClass> {
@Override
public void serialize(MyClass myClass, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("name", myClass.getName());
// write other properties
jsonGenerator.writeEndObject();
}
}
@RestController
public class MyController {
@Autowired
private MyService myService;
@GetMapping("/my-endpoint")
public MyClass getMyClass() {
return myService.getMyClass();
}
}
以上是在Spring JSON查询中不发送@IdClass对象的几种方法。根据具体情况选择适合的方法来实现需求。对于Spring JSON查询的更多信息和使用方法,可以参考腾讯云的Spring Cloud产品:Spring Cloud。
领取专属 10元无门槛券
手把手带您无忧上云