0

I have controller for post request:

@RequestMapping(value="/projects/add", method=RequestMethod.POST)
@ResponseBody
public String addProject(@Valid @RequestBody usersProjects project, BindingResult result) {
    if(result.hasErrors()) {
        System.out.println("Error ::: " +result.toString());
    }

    logger.info(project.toString());

    usersProjects.addProject(project);

    return "ok";
}

Entity for database table:

@Entity
@Table(name="users_projects")
public class UsersProjects {

    @GeneratedValue(strategy=GenerationType.AUTO)
    @Id
    private Long id;

    @NotEmpty
    private String name;

    private Boolean approved;

    @NotEmpty
    @ElementCollection
    private List<String> spaces;
    @NotEmpty
    @ElementCollection
    private List<String> owners;

    @OneToMany(cascade=CascadeType.ALL, 
             fetch=FetchType.EAGER)
    private Set<UsersProjectJobs> jobs;

    @OneToMany(cascade=CascadeType.ALL, 
             fetch=FetchType.EAGER)
    private Set<UsersProjectData> data;


    public UsersProjects() {

    }

    public UsersProjects(String name, List<String> spaces, List<String> owners) {
        this.name = name;
        this.spaces = spaces;
        this.owners = owners;
        this.approved = false;
    }

    public Boolean getApproved() {
        return approved;
    }
    public void setApproved(Boolean approved) {
        this.approved = approved;
    }
}

When I'm trying to POST data to server with CURL:

curl -i  -H "Content-Type:application/json" -H "Accept:application/json"  -X POST -d '{ "name": "123", "spaces": ["123"], "owners": ["123"]}' http://localhost:8080/projects/add

And got error:

The request sent by the client was syntactically incorrect.

I have found a lot of topic where described that this error occurs when Spring can't validate received JSON data. What I should add to log4j.properties for more verbose output or maybe someone knows link to good topic about model validation?

Thank you.

UPD log4j configuration:

# Root logger option
log4j.rootLogger=DEBUG, stdout

log4j.logger.org.springframework=DEBUG, stdout
log4j.logger.org.springframework.beans=DEBUG, stdout
log4j.logger.org.springframework.context=DEBUG, stdout
log4j.logger.org.springframework.http=DEBUG, stdout
log4j.logger.org.springframework.web=DEBUG, stdout
log4j.logger.org.hibernate=DEBUG, stdout
log4j.category.org.springframework.beans.factory=DEBUG, stdout

log4j.logger.org.apache.catalina.core=DEBUG
log4j.logger.org.apache.catalina.session=DEBUG

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

UPD UPD:

public class validatorAddProject {
    private String name;
    private List<String> spaces;
    private List<String> owners;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<String> getSpaces() {
        return spaces;
    }
    public void setSpaces(List<String> spaces) {
        this.spaces = spaces;
    }
    public List<String> getOwners() {
        return owners;
    }
    public void setOwners(List<String> owners) {
        this.owners = owners;
    }
}


@RequestMapping(value="/projects/add", method=RequestMethod.POST)
@ResponseBody
public String addProject(@Valid @RequestBody validatorAddProject form, BindingResult result) {
    if(result.hasErrors()) {
        System.out.println("Error ::: " +result.toString());
    }

    usersProjects project = new usersProjects(form.getName(), form.getSpaces(), form.getOwners());
    usersProjects.addProject(project);

    return "ok";
}
8
  • Is that the full UsersProjects class? Is that a type in the handler method? Turn your logs to DEBUG and look again. Commented Jun 20, 2014 at 0:57
  • My log4j.properties file: pastebin.com/6KuuYT61 I don't see here any records about validation errors. Yes, it's full entity class. Commented Jun 20, 2014 at 1:00
  • Your logs should contain information about why you got a 400. Check them. If they don't, your logger is misconfigured. Commented Jun 20, 2014 at 1:03
  • @AlexanderGryanko Please add your log4j.properties to the post, don't let people follow a link to get all the information. Commented Jun 20, 2014 at 6:30
  • If that is your full class you are missing some getters/setters so no binding will be done. Unless you configured Jackson to do field based binding, by default it does property (get/set) binding. Commented Jun 20, 2014 at 6:31

2 Answers 2

1

there is a nice project that shows how to handle e.g. validation errors. Just have a look at the MethodArgumentNotValidExceptionHandler

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but its not what I want. I've found solution by adding slf4j to project.
0

I think you need to add setters or decorate your constructor with @JsonCreator. Also upgrading to the latest Boot version, if you are using that (I'm guessing from the tags), should expose the BindingException in the response if that's what it is.

1 Comment

Thank you. I have added JsonCreator and JsonProperty to the Entity class, it works but not like I want. I've created another one public class only with fields what I want to add to database.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.