2

I have the following REST service that receives a POJO/Bean as a parameter but it's name in the Bean Validation JSON response is presented as arg0.

How can I give it a custom name like "request"?

REST Service

@Path("/service")
public class MyRequestResource {

    @POST
    public void post(@Valid MyRequest request) {
        /* do stuff here */
    }

}

The POJO/Bean (@BeanParam if you will)

public class MyRequest {

    @NotNull
    private LocalDate date;

}

Bean Validation response

As we can see the response JSON is returning "arg0" as the name of the bean param. How can I change the name for something custom like "request"?

{
    "exception": null,
    "fieldViolations": [],
    "propertyViolations": [],
    "classViolations": [],
    "parameterViolations": [
        {
            "constraintType": "PARAMETER",
            "path": "post.arg0.date",
            "message": "may not be null",
            "value": ""
        }
    ],
    "returnValueViolations": []
}
Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
  • In your ide settings, there should be an option to retain parameter names or metadata or something like that. Do you use eclipse? – vikarjramun Nov 17 '17 at 20:03
  • @vikarjramun this is at runtime... your comment makes no sense – Evandro Pomatti Nov 20 '17 at 12:59
  • @BonanzaOne, it actually does make sense, parameter names metadata is preserved at runtime with correct flags. That said, try adding a like a `@FormParam("request")` annotation, maybe? – M. Prokhorov Nov 20 '17 at 15:10
  • @BonanzaOne The reason I'm saying this is because when you compile into class files, sometimes you lose parameter and variable names. Thus when they are accessed by reflection, you can't get their original names, and instead get arg0 or arg1... Although if you use maven or such, I have no idea how to go about keeping parameter names – vikarjramun Nov 20 '17 at 15:18
  • If you use maven: ` org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 -parameters ` – vikarjramun Nov 20 '17 at 16:02
  • @M.Prokhorov it gives me an error: `RESTEASY003875: Unable to find a constructor that takes a String param or a valueOf() or fromString() method for javax.ws.rs.FormParam("request")` – Evandro Pomatti Nov 21 '17 at 12:07
  • @vikarjramun the Maven parameter didn't work. For JAX-WS SOAP Web Services there is the `@WebParam` that does that, I am looking for something similar for JAX-RS. – Evandro Pomatti Nov 21 '17 at 12:13
  • @BonanzaOne look at the question Georg Henkel marked as duplicate. There is apparently a bit more to do before BeanValidation realizes that it has names available. – vikarjramun Nov 21 '17 at 13:55
  • @vikarjramun I see. In fact the question is a duplicate, but too much work just for that. I would expect something simpler. Thanks! – Evandro Pomatti Nov 21 '17 at 16:12
  • @BonanzaOne To be honest, if you already have a validation.xml, it's not that much work that you should skip doing it. If I were you, I would implement that solution – vikarjramun Nov 22 '17 at 17:50

0 Answers0