Solution 1: As my TextSerializer class contains @Autowired field, it must not be created with new
operator. It must be retrieved from Spring container. But field textSerializer
in my JacksonMapper
class is null.
Solution 2: If i create textSerializer
with new
operator, then sessionFactory
inside is null.
I think that solution 1 is more right and problem is in JsonVisitable
interface. But my textSerializer
field don't want autowire, if it's type will be class(TextSerializer
or JsonSerializer
).
public class JacksonMapper extends ObjectMapper {
@Autowired
private JsonFormatVisitable textSerializer;
public JacksonMapper() {
SimpleModule module=new SimpleModule("SimpleModule");
module.addSerializer(Text.class, (JsonSerializer<Object>) textSerializer);
..............................
registerModule(module);
}
}
public class TextSerializer extends JsonSerializer<Text> {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public void serialize(Text value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
.........
}
}
@Configuration
@EnableWebMvc
@ComponentScan({"xxx.service", "xxx.controller" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
.........................
@Bean
public JacksonMapper jacksonMapper(){
return new JacksonMapper();
}
@Bean
public TextSerializer textSerializer(){
return new TextSerializer();
}
}