英文:
is it possible to convert long to string in spring boot rest api without using enablewebmvc annotation
问题
Because JavaScript cannot handle the long data type, you are converting the backend Java Long data type to a string. Your code configuration appears correct, but it seems it's not working. To make the conversion work without using the EnableMvc
annotation, you may want to check the following:
-
Ensure that the
WebConvertConfig
class is being picked up by the Spring application context. Make sure it's in a package that gets scanned during component scanning. -
Verify that there are no conflicting configurations or interceptors that might interfere with your custom message converter.
-
Double-check that you have imported the necessary classes, such as
ObjectMapper
,SimpleModule
, andMappingJackson2HttpMessageConverter
. -
Make sure that the URL you are accessing is returning data that should be converted. Check if the controller or service method is correctly returning a
Long
value. -
If you are using Spring Boot, check if you have any other auto-configurations or dependencies that might be affecting the message conversion.
-
Verify your application's logs for any error messages or warnings related to message converters or Jackson configuration.
By reviewing these aspects, you can troubleshoot why your conversion might not be working as expected.
英文:
because the javascript could not handle the long data type , so I need to convert the backend java Long data type to string, is it possible to do like this? I am doing it like this:
package com.dolphin.soa.post.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Collections;
import java.util.List;
@Configuration
public class WebConvertConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(new StringHttpMessageConverter());
converters.add(jackson2HttpMessageConverter);
converters.add(createEventStreamMessageConverter());
}
private HttpMessageConverter<Object> createEventStreamMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_EVENT_STREAM));
return converter;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
seems did not work. what should I do to make the convert work without using EnableMvc annotation?
答案1
得分: 1
使用 Jackson2ObjectMapperBuilderCustomizer
对我有效,类似以下方式:
@Configuration
class JacksonLongConfig implements Jackson2ObjectMapperBuilderCustomizer {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.serializerByType(Long.class, ToStringSerializer.instance);
}
}
英文:
Using a Jackson2ObjectMapperBuilderCustomizer
works for me, something like:
@Configuration
class JacksonLongConfig implements Jackson2ObjectMapperBuilderCustomizer {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.serializerByType(Long.class, ToStringSerializer.instance);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论