6

I have a Spring 3.2 application and I've created a REST API following this example based on Spring MVC. Now I am experiencing some issues while trying to validate some data for different http methods (e.g.: POST and PUT methods).

This would be a pretty simplified example:

public class myItem{

    @NotEmpty
    private String foo;

    @NotEmpty
    private String bar;

    public myItem(String foo){
        this.foo = foo;
        this.bar = "";
    }

    public myItem(String foo, String bar){
        this.foo = foo;
        this.bar = bar;
    }

    /* getters & setters omitted */

}

This POJO is reused in the different request methods.

This would be my simplified controller:

@Controller
@RequestMapping("/api/item")
public class myItemController{

    @RequestMapping(value="/", method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED) 
    public @ResponseBody myItem createItem(@Valid @RequestBody myItem item){
        /* do some stuff */
        return item; //inserted item returned
    }

    @RequestMapping(value="/", method=RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public @ResponseBody myItem createItem(@Valid @RequestBody myItem item){
        /* do some stuff */
        return item //updated item returned
    }
}

In the POST method I only expect foo field to be set, so this request would fail with the before above annotations. In the PUT method I expect both foo and bar fields to be set, so this request would be completed successfully.

What is the correct approach to deal with this kind of situations: in a certain request method you don't expect all fields to be filled up (some fields may have default values, hence you don't want to check all them, aka create), and in another method you must check all the fields (aka update).

4

1 回答 1

9

使用验证组:

public interface ValidateOnCreate {}
public interface ValidateOnUpdate {}

.

public class myItem{ 
    @NotEmpty(groups = { ValidateOnCreate.class, ValidateOnUpdate.class })
    private String foo;

    @NotEmpty(groups = ValidateOnUpdate.class)
    private String bar;
    ...
}

.

@RequestMapping(value="/", method=RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED) 
public @ResponseBody myItem createItem(
    @Validated(ValidateOnCreate.class) @RequestBody myItem item) { ... }

@RequestMapping(value="/", method=RequestMethod.PUT)
@ResponseStatus(HttpStatus.NO_CONTENT)
public @ResponseBody myItem createItem(
    @Validated(ValidateOnUpdate.class) @RequestBody myItem item) { ... }

请注意,@Validated在这种情况下您需要特定于 Spring,因为@Valid不允许您指定组。

也可以看看:

于 2013-07-03T16:32:49.887 回答