2

I have a class like this:

@JsonSerialize(using=MatchedArticle.class)
public class MatchedArticle extends JsonSerializer<MatchedArticle>{

It builds fine with maven, but Eclipse reports the following error:

Type mismatch: cannot convert from Class<MatchedArticle> to Class<? extends JsonSerializer<?>>

which is weird because MatchedArticle really do extend JsonSerializer

Any hints on how to turn off this exact validation in Eclipse?

2
  • I came across this question only now. I tried to reproduce the problem, but couldn't. If you still see this: which Eclipse version are you using? What compiler settings (compliance)? Jul 19, 2016 at 10:15
  • came through the same bug (on Jackson @JsonSerialize too), if you've found your answer, please share it with us ! Dec 19, 2016 at 16:13

4 Answers 4

3

Came through the same issue.You may have imported the wrong class/package.

In my case Mistakenly I imported this

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

instead of this correct solution

import org.codehaus.jackson.map.annotate.JsonSerialize;

Hope this helps!

1
  • Thanks. That may have solved my problem, but I handled it differently. I'll look into it if I have the time. Oct 19, 2017 at 10:10
0

In case anyone stumbles upon this question. I have not tried out the solution given by Vijay. It would be natural to try that out first.

I solved it by extending SimpleModule, and inside it I added a few nested empty mixin classes.

public class MyModule extends SimpleModule {

      @JsonSerialize(using = ObjectSerializer.class)
      @JsonDeserialize(using = ObjectDeserializer.class)
      public static class ObjectMixin {}

      @JsonSerialize(using = NaturalKeySerializer.class)
      @JsonDeserialize(using = NaturalKeyDeserializer.class)
      public static class NaturalKeyMixin {}

      @JsonSerialize(using = ChangeSerializer.class)
      @JsonDeserialize(using = ChangeDeserializer.class)
      public static class ChangeMixin {}

      @JsonSerialize(using = ViolationSerializer.class)
      public static class ViolationMixin {}
}
0

I have come across the same issue. Here are my 2 classes in a very much larger project:

package ws.daley.hollow.validation;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

@Target({ TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = PasswordMatchesValidator.class)
@Documented
public @interface PasswordMatches
{
    String message() default "Passwords don't match";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

and

package ws.daley.hollow.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import ws.daley.hollow.persistence.admin.model.User;
import ws.daley.hollow.web.admin.dto.UserDto;

public class PasswordMatchesValidator implements ConstraintValidator<PasswordMatches, Object>
{
    @Override
    public void initialize(@SuppressWarnings("unused") final PasswordMatches constraintAnnotation) {/* */}

    @Override
    public boolean isValid(final Object obj, final ConstraintValidatorContext context)
    {
        @SuppressWarnings("unchecked")
        final UserDto<User> user = (UserDto<User>) obj;
        return user.getPassword().equals(user.getMatchingPassword());
    }
}

The following line in PasswordMatches

    @Constraint(validatedBy = PasswordMatchesValidator.class)

gets the error:

Type mismatch: cannot convert from Class<PasswordMatchesValidator> to Class<? extends ConstraintValidator<?,?>>[]

The code builds fine under maven both inside and outside of STS4.

I have found a temporary (and very transient) fix for the problem. Simply copy the PasswordMatches source code to the clipboard, delete the PasswordMatches class from the project, add a new class of the same name, paste the copied code back in, save and build. The problem is now gone, but only for a while. Sometime later, apparently some random occurrence causes the exact same error to reappear in exactly the same place. The above action again fixes the problem.

I am running STS4 on Eclipse. The problem did not occur before I upgraded to JDK10 from JDK8 and from STS3 to STS4, although this may be anecdotal. I suspect the problem is in the new eclipse/STS4 or it's use of language servers.

0

I faced this problem while serializing enum constants. I resolved this problem by creating a custom module and adding serializer, deserializers to it.

public enum Month {
 SUNDAY("Sunny Sunday"), MONDAY("Morning Monday"), TUESDAY("Good tuesday"), WEDNESDAY("Happy Wednesday"),
 THURSDAY("Great Tuesday"), FRIDAY("Good Friday"), SATURDAY("Sleepy Saturday");

 .....
 .....
}

public class MonthSerializer extends JsonSerializer<Month> {

 @Override
 public void serialize(Month month, JsonGenerator gen, SerializerProvider serializers)
   throws IOException, JsonProcessingException {
   .....
   .....
 }

}

public class MonthDeserializer extends JsonDeserializer<Month> {

 @Override
 public Month deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {

 .....
 .....
 }
}

public class MonthModule extends SimpleModule{
     private static final long serialVersionUID = 1L;

     private static final String NAME = "CustomAddressModule";
     private static final VersionUtil VERSION_UTIL = new VersionUtil() {
     };

     public MonthModule() {
             super(NAME, VERSION_UTIL.version());
             addSerializer(Month.class, new MonthSerializer());
             addDeserializer(Month.class, new MonthDeserializer());
     }
}

Once you defined MonthModule, register it to the ObjectMapper.

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new MonthModule());

Referene link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.