Skip to content

Add Support for HttpMethod serialization in Spring 6 #33870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mwcodeslave opened this issue Nov 11, 2024 · 3 comments
Closed

Add Support for HttpMethod serialization in Spring 6 #33870

mwcodeslave opened this issue Nov 11, 2024 · 3 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@mwcodeslave
Copy link

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());
      }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 11, 2024
@mwcodeslave mwcodeslave changed the title Add Support for HttpMethod for serialization in Spring 6 Add Support for HttpMethod serialization in Spring 6 Nov 11, 2024
@jhoeller jhoeller added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 11, 2024
@lucky8987
Copy link
Contributor

@mwcodeslave Can adding public String getName() { return this.name; } to HttpMethod solve your problem? But this will lead to another issue, the serialization of GET objects will be {"name":"GET"} instead of "GET". I'm not sure if this is what you want.

@bclozel
Copy link
Member

bclozel commented Nov 13, 2024

@lucky8987 thanks for helping out! Indeed, it' looks like we cannot make this class "Jackson-friendly" and have a similar serialization format without breaking the current API.

@mwcodeslave I think the serializer implementations you've shared look like the expected solution. Framework provides many classes in the org.springframework.http package and none of them are expected to be used to write/read JSON payloads. I don't think we're advising developers to use such classes as part of their payloads. I'm not seeing an easy way out for this; we could add Jackson-specific annotations or implementations but we would need to do so for entire packages to be consistent. Sharing serializer implementations like the one suggested above also feel outside of the scope of this project.

I'm closing this issue for now; developers should use your implementations if they wish to retain the serialization behavior as it was. Thanks!

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2024
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 13, 2024
@lucky8987
Copy link
Contributor

@bclozel I agree with your suggestion, and if necessary, I can add this custom HttpMethodSerializer to the unit test, which can provide reference for those who encounter the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

5 participants