1

I am using Springboot 2.1.2.RELEASE. I have a get request with an object as input parameter. Expecting the attributes in my class to be request parameters. My EmployeeBean has properties in java naming convention. But I need the custom names to request parameters. Tried to achieve that using @JsonProperty/ @Jsongetter/ @JsonSetter annotations but its not working. Am I missing something?

@RequestMapping(value="/api", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE )
public List<Map<String, Object>> getEmployeeData(EmployeeBean employeeBean


@Data
public class EmployeeBean implements Serializable {

private static final long serialVersionUID = -2757478480787308113L;

@JsonProperty(value="first_name")
private String firstName;

@JsonProperty(value="last_name")    
private String lastName;
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
Vinay
  • 21
  • 4
  • Without the `@JsonProperty`s, does it work? – acdcjunior Sep 24 '19 at 22:16
  • Which version of `Jackson` do you use and from which package you use imports: `org.codehaus.jackson` or `com.fasterxml.jackson.core`. See [org.codehaus.jackson versus com.fasterxml.jackson.core](https://stackoverflow.com/questions/30782706/org-codehaus-jackson-versus-com-fasterxml-jackson-core). Also, show `JSON` payload you want to deserialise. – Michał Ziober Sep 24 '19 at 23:12
  • @acdcjunior It works without the Jsonproperty but the json element is the name of the variable. – Vinay Sep 25 '19 at 14:23
  • 1
    @MichałZiober I am using fasterxml jackson 2.9.8. {"first-name":"xyz", "last-name":"abc"}. – Vinay Sep 25 '19 at 14:25
  • @Vinay, in annotation you have `@JsonProperty(value="first_name")` but in `JSON` payload `"first-name"`. `-` vs `_` - it is not the same char. – Michał Ziober Sep 25 '19 at 14:34
  • @MichałZiober. Thanks but this is just an example. I am fine with _ or - :). Can't share project code here :P. I will be kicked out of the job :P – Vinay Sep 25 '19 at 15:56

1 Answers1

0

Try this,

    private String firstName;

    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @JsonProperty(value="first_name")
    public void setFirst_name(String firstName) {
        this.firstName = firstName;
    }

    @JsonProperty(value="last_name")
    public void setLast_name(String lastName) {
        this.lastName = lastName;
    }

controller

@RestController
public class JsonController {

    @RequestMapping(value="/api", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE )
    public List<Map<String, Object>> getEmployeeData(EmployeeBean employeeBean) {
        System.out.println("employeeBean: "+employeeBean);
        return null;
    }
}

enter image description here

result:

employeeBean: EmployeeBean [firstName=firstName10, lastName=lastName20]

I've tested and it's worked

other options, using JsonCreator in constructor:

    private String firstName;

    private String lastName;

    @JsonCreator
    public EmployeeBean(@JsonProperty("first_name") String first_name, @JsonProperty("last_name") String last_name) {
        this.firstName = first_name;
        this.lastName = last_name;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
Erwin
  • 460
  • 2
  • 6
  • Thanks. Which version of Jackson are you using? I tried this but it didn't work for me. – Vinay Sep 25 '19 at 15:50
  • I am using spring-boot 2.1.8.RELEASE – Erwin Sep 25 '19 at 15:56
  • I am using 2.1.2.RELEASE with Greenwich.RELEASE – Vinay Sep 25 '19 at 16:07
  • I'm running mvn dependency:tree and I think it using Jackson 2.9.9 – Erwin Sep 25 '19 at 16:27
  • This is weird. The same is not working for me. I have changed to 2.1.8 which has jackson 2.9.9. Did you try this object as RequestBody or as RequestParameter? I am using this as requestparamter in a GET call. – Vinay Sep 25 '19 at 16:47
  • I didn't change your method. I have updated my answer. – Erwin Sep 25 '19 at 16:57
  • I see now. You are using setFirst_name() & setLast_name instead of setFirstName & setLastName. That's why its working. But this is a deviation from java naming convention which I am trying to avoid. – Vinay Sep 25 '19 at 18:01
  • Seem there's no other way. The annotation seem to need the method name to be equal with property name. That's why I still write the getter & setter that follow the naming convention. – Erwin Sep 25 '19 at 18:45
  • I've found other way. Create a constructor with JsonCreator, so it is not deviate from java naming convention, still for the specific constructor you should use property names which is the same with the jsonproperty. I have updated my answer. Hope this work. – Erwin Oct 24 '19 at 06:56