is it possible to convert long to string in spring boot rest api without using enablewebmvc annotation

huangapple go评论76阅读模式
英文:

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:

  1. 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.

  2. Verify that there are no conflicting configurations or interceptors that might interfere with your custom message converter.

  3. Double-check that you have imported the necessary classes, such as ObjectMapper, SimpleModule, and MappingJackson2HttpMessageConverter.

  4. 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.

  5. If you are using Spring Boot, check if you have any other auto-configurations or dependencies that might be affecting the message conversion.

  6. 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);
	}
}

huangapple
  • 本文由 发表于 2023年5月26日 01:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定