1

First off, I used JAXB to create the java classes from an XSD document, and this created the following structure

import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;
import javax.xml.bind.annotation.XmlType;

@XmlType(
name = "types"
)

@XmlEnum
public enum Types {
      @XmlEnumValue("tp1")
      TP1("tp1"),
      @XmlEnumValue("tp2")
      TP2("tp2")

private final String value;

private Types(String v) {
    this.value = v;
}

public String value() {
    return this.value;
}

public static Types fromValue(String v) {
    Types[] arr$ = values();
    int len$ = arr$.length;

    for(int i$ = 0; i$ < len$; ++i$) {
        Types c = arr$[i$];
        if (c.value.equals(v)) {
            return c;
        }
    }
    throw new IllegalArgumentException(v);
  }
}

So when i pass in paylod for my api in SpringBoot

@Override
@RequestMapping(method = RequestMethod.POST, value = "/create",
        produces = "application/json",consumes = "application/json")
public Task  createTask(@Valid @RequestBody Task taskRequest) throws 
Exception {
    return taskService.createTask(taskRequest);
}

and payload is

{
"type": "tp1",
"partner": {
"partnerId": 1234567,
   "partnerTransactionId": "808c7c1d-e43e-4804-a192-00a61deef321"
  }
 }

Then I get error

{
  "timestamp": 1514695697219,
  "status": 400,
  "error": "Bad Request",
  "exception": 
  "org.springframework.http.converter.HttpMessageNotReadableException",
  "message": "JSON parse error: Can not deserialize value of type 
   com.v1.Type from String \"tp1\": value not one of declared Enum instance 
   names:[TP1,TP2]; nested exception is 
   com.fasterxml.jackson.databind.exc.InvalidFormatException
   "path": "/test/services/v1/create"
 }

I tried to use spring.jackson.deserialization.fail-on-unknown-properties=false But it doesn't help.

How to solve this issue ?

James Z
  • 12,209
  • 10
  • 24
  • 44
neo
  • 47
  • 1
  • 7
  • sorry, but I am a bit confused. You are using jackson/json to be used in your controller. Why are you using jaxb annotations? – Andrei Sfat Dec 31 '17 at 09:47
  • Possible duplicate of [Jackson enum Serializing and DeSerializer](https://stackoverflow.com/questions/12468764/jackson-enum-serializing-and-deserializer) – Andrei Sfat Dec 31 '17 at 09:51
  • @sfat As I said I am using a dependent jar for all the schema and they used xsd to generated the class.Since changing all the generated classes from xsd is meaningless – neo Dec 31 '17 at 13:44

0 Answers0