I have a CRUD service in Spring Boot, and I'm using a custom serializer with a class that extends JsonSerializer
. I want to use this CRUD service when my class is serialized, but I've tried several approaches without success. Could you help me with this in a forum?"
@Component// Reemplaza @Component para que Spring registre este serializadorpublic class Template1Serializer extends JsonSerializer<Template1> implements ApplicationContextAware {
//private static ApplicationContext applicationContext;
private CustomerUserServiceIMPL customerUserServiceIMPL;
public Template1Serializer() {
// Obtener el servicio manualmente desde el contexto de Spring
// this.customerUserServiceIMPL = ApplicationContextProvider.getBean(CustomerUserServiceIMPL.class);
}
@PostConstruct
public void init() {
// Usamos ApplicationContextProvider para obtener el servicio después de la inicialización de Spring
this.customerUserServiceIMPL = ApplicationContextProvider.getBean(CustomerUserServiceIMPL.class);
}
/* @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {applicationContext = context;this.customerUserServiceIMPL = applicationContext.getBean(CustomerUserServiceIMPL.class);}*/
@Override
public void serialize(Template1 template1, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String usuario=";";
if (this.customerUserServiceIMPL == null) {
System.out.println("CustomerUserServiceIMPL es null en serialize()");
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("body", template1.getTemplateText() + " - Mensaje añadido serializador"+usuario+"no hay nada");
jsonGenerator.writeEndObject();
} else {
System.out.println("CustomerUserServiceIMPL está disponible en serialize()");
String usuario2=getuser();
jsonGenerator.writeStartObject();
System.out.println("deberia añadir "+usuario2);
jsonGenerator.writeStringField("body", template1.getTemplateText() + " - Mensaje añadido serializador"+usuario2+"final");
jsonGenerator.writeEndObject();
}
}
private String getuser() {
System.out.println("Servicio ejecutado");
Optional<valueContacts> profiletest = customerUserServiceIMPL.getCustomerFromPhonenunmber("573248324121");
if (profiletest.isPresent()) {
System.out.println(profiletest.get().getCustomerName());
System.out.println(profiletest.get().getCedula());
String retorno="hola querido usuario tu nombre es "+profiletest.get().getCustomerName()+"y tu cedula es"+profiletest.get().getCedula();
return retorno;
} else {
System.out.println("No se encontró el perfil.");
return null;
}
}
I tried using @Autowired
and ApplicationContextProvider
, but I couldn't make it work."
This version makes your attempt and the challenge you're facing clearer. Would you like help refining the approach you're using for the serializer?
I have a CRUD service in Spring Boot, and I'm using a custom serializer with a class that extends JsonSerializer
. I want to use this CRUD service when my class is serialized, but I've tried several approaches without success. Could you help me with this in a forum?"
@Component// Reemplaza @Component para que Spring registre este serializadorpublic class Template1Serializer extends JsonSerializer<Template1> implements ApplicationContextAware {
//private static ApplicationContext applicationContext;
private CustomerUserServiceIMPL customerUserServiceIMPL;
public Template1Serializer() {
// Obtener el servicio manualmente desde el contexto de Spring
// this.customerUserServiceIMPL = ApplicationContextProvider.getBean(CustomerUserServiceIMPL.class);
}
@PostConstruct
public void init() {
// Usamos ApplicationContextProvider para obtener el servicio después de la inicialización de Spring
this.customerUserServiceIMPL = ApplicationContextProvider.getBean(CustomerUserServiceIMPL.class);
}
/* @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {applicationContext = context;this.customerUserServiceIMPL = applicationContext.getBean(CustomerUserServiceIMPL.class);}*/
@Override
public void serialize(Template1 template1, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String usuario=";";
if (this.customerUserServiceIMPL == null) {
System.out.println("CustomerUserServiceIMPL es null en serialize()");
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("body", template1.getTemplateText() + " - Mensaje añadido serializador"+usuario+"no hay nada");
jsonGenerator.writeEndObject();
} else {
System.out.println("CustomerUserServiceIMPL está disponible en serialize()");
String usuario2=getuser();
jsonGenerator.writeStartObject();
System.out.println("deberia añadir "+usuario2);
jsonGenerator.writeStringField("body", template1.getTemplateText() + " - Mensaje añadido serializador"+usuario2+"final");
jsonGenerator.writeEndObject();
}
}
private String getuser() {
System.out.println("Servicio ejecutado");
Optional<valueContacts> profiletest = customerUserServiceIMPL.getCustomerFromPhonenunmber("573248324121");
if (profiletest.isPresent()) {
System.out.println(profiletest.get().getCustomerName());
System.out.println(profiletest.get().getCedula());
String retorno="hola querido usuario tu nombre es "+profiletest.get().getCustomerName()+"y tu cedula es"+profiletest.get().getCedula();
return retorno;
} else {
System.out.println("No se encontró el perfil.");
return null;
}
}
I tried using @Autowired
and ApplicationContextProvider
, but I couldn't make it work."
This version makes your attempt and the challenge you're facing clearer. Would you like help refining the approach you're using for the serializer?
Share Improve this question asked yesterday juancamilo ariasotalvarojuancamilo ariasotalvaro 112 bronze badges New contributor juancamilo ariasotalvaro is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 0I don't think it's possible to inject anything in a Jackson serializer. Jackson isn't aware of any Spring Boot or Jakarta EE injection mechanisms.
However, Spring Boot allows you to provide modules as beans, and these will automatically be registered with the ObjectMapper
that Spring Boot provides.
The simplest (but perhaps not cleanest) way is to make your serializer, which is already a bean of itself, provide the module:
@Bean
public Module jacksonTemplateModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(this);
return module;
}
A cleaner solution would be to make your module be the bean instead. Inject the service into the module bean, and pass the service to the serializer instance through a constructor, just as if it were any normal Java object.