Skip to content

Add Support for HttpMethod serialization in Spring 6 #33870

Closed as not planned
Closed as not planned
@mwcodeslave

Description

@mwcodeslave

As mentioned here: #27697 (comment)

Since migrating from Spring Boot 2.7.x to 3.3.x the jackson ObjectMapper is not able to serialize org.springframework.http.HttpMethod
anymore. You have to write now your own modules with serializer and deserializer now.

public class JacksonConfig{

    @Bean
    SimpleModule httpMethodModule() {
        SimpleModule module = new SimpleModule();
        module.addSerializer(HttpMethod.class, new HttpMethodSerializer());
        module.addDeserializer(HttpMethod.class, new HttpMethodDeserializer());
        return module;
    }
    
    @Bean
    Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> builder
                .modules(httpMethodModule());
    }
}

public class HttpMethodSerializer extends JsonSerializer<HttpMethod> {
      @Override
      public void serialize(HttpMethod httpMethod, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException {
          jsonGenerator.writeString(httpMethod.name());
      }
}
    
public class HttpMethodDeserializer extends JsonDeserializer<HttpMethod> {
      @Override
      public HttpMethod deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
          return HttpMethod.valueOf(jsonParser.getText().toUpperCase());
      }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions