0

I am having two Spring Rest service create-employee and create-staff like as shown below

create-employee

@RequestMapping(value="/create-employee", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<Void> createEmployee(final @RequestBody User user) {
    try {
        // employee createion logic
    } catch (Exception exception) {
        log.error("Exception in createEmployee:"+exception.getMessage());
        return new ResponseEntity<>(HttpStatus.FORBIDDEN);
    }
}

create-staff

@RequestMapping(value="/create-staff", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<Void> createStaff(final @RequestBody User user) {
    try {
        // staff creation logic
    } catch (Exception exception) {
        log.error("Exception in createStaff:"+exception.getMessage());
        return new ResponseEntity<>(HttpStatus.FORBIDDEN);
    }
}

For both the services I am using a dto named User like as shown below:

public class User {
    @JsonProperty("employeeName")
    private String name;

    @JsonProperty("age")
    private Integer age;

    @JsonProperty("managerName")
    private String headName;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getHeadName() {
        return headName;
    }

    public void setHeadName(String headName) {
        this.headName = headName;
    }
}

Now the issue is that for both create-employee and create-staff since I am using User.java as the request body. The posting json body looks like this

{
 "employeeName" : "string",
 "age" : "integer",
 "managerName" : "string"
}

but actually what I want is that for create-staff service I would like to have the json body as below

{
 "staffName" : "string",
 "age" : "integer",
 "managerName" : "string"
}

and create-staff service I would like to have the json body as below

{
 "employeeName" : "string",
 "age" : "integer",
 "managerName" : "string"
}

But for both the services I need to use the same User.java dto but with different JsonProperty for the two services

Can anyone please hep me on this

Alex Man
  • 4,746
  • 17
  • 93
  • 178
  • What if you just used `name` and removed any need to do this? – Nick Nov 20 '17 at 04:21
  • 1
    @NickPredey You are right, but actually this was a client requirement and not only this many other services are there of similar scenarios . Like to know whether there is any way we can conditionally change the jsonproperty for staff and employee. – Alex Man Nov 20 '17 at 04:24
  • @AlexMan : you're trying to resolve an design problem by forcing the Json concept. Why don't have an member property `Statut statut` in the `User`. `enum Statut{ STAFF, USER, MANAGER, ....}` – Zorglube Nov 20 '17 at 16:52
  • @AlexMan you can add two property member in your user object and tell to Jackson to not return `null` value. `class User { String employeeName; String staffName; ... }`. Look here https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null – Zorglube Nov 20 '17 at 17:13

1 Answers1

0

Jackson also supports JsonAlias which might be helpful for you, just make sure you updated your jacskon mapper to version 2.9.1

public class User {
    @JsonAlias({"employeeName","staffName"})
    private String name;

    @JsonProperty("age")
    private Integer age;

    @JsonProperty("managerName")
    private String headName;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getHeadName() {
        return headName;
    }

    public void setHeadName(String headName) {
        this.headName = headName;
    }
}
Alexander.Furer
  • 1,817
  • 1
  • 16
  • 24
  • you are right its request body and not response. with JsonView how we can do this conditionally – Alex Man Nov 20 '17 at 04:28
  • You have the sample in the documentation https://github.com/FasterXML/jackson-annotations/blob/master/src/main/java/com/fasterxml/jackson/annotation/JsonAlias.java – Alexander.Furer Nov 20 '17 at 04:53
  • using `JsonAlias` we can give multiple alias names for a property like `@JsonAlias({ "staffName", "employeeName" }) public String name;`. But how we can dynamically specify as `employeeName` property should come for **create-employee** service and `staffName` property should appear for **create-staff** service. – Alex Man Nov 20 '17 at 06:28