<!-- Jersey MVC Bean Validator -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-bean-validation</artifactId>
<version>${jersey-mvc}</version>
</dependency>
package com.mycompany.jerseymvcexam;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.beanvalidation.MvcBeanValidationFeature;
import org.glassfish.jersey.server.mvc.mustache.MustacheMvcFeature;
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
this
.packages(ApplicationConfig.class.getPackage().getName())
.register(MvcBeanValidationFeature.class)
//.property(MustacheMvcFeature.TEMPLATE_BASE_PATH, "/Users/atsushi/templates")
//.property(MustacheMvcFeature.ENCODING, "UTF-8")
.register(MustacheMvcFeature.class);
}
}
package com.mycompany.jerseymvcexam;
import javax.ejb.EJB;
import javax.validation.Valid;
import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.server.mvc.ErrorTemplate;
import org.glassfish.jersey.server.mvc.Template;
@Path("board2")
public class BoardResource2 {
@EJB
private BoardEJB board;
@GET
@Path("init")
@Produces(MediaType.TEXT_HTML)
@Template(name = "/board")
public BoardBean init() {
BoardBean res = new BoardBean();
res.setName("");
res.setComment("");
res.setArticles(board.getCurrentMessage());
return res;
}
@POST
@Path("submit")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
@Template(name = "/board")
@ErrorTemplate(name = "/board_error")
public BoardBean submitMessage(@Valid @BeanParam ContributeBean contribute) {
String name = contribute.getName();
String comment = contribute.getComment();
board.addMessage(name, comment);
BoardBean res = new BoardBean();
res.setName(name);
res.setComment(comment);
res.setArticles(board.getCurrentMessage());
return res;
}
}
package com.mycompany.jerseymvcexam;
import javax.validation.constraints.Size;
import javax.ws.rs.FormParam;
import lombok.Data;
@Data
public class ContributeBean {
@FormParam("name")
@Size(min = 1, max = 255)
private String name;
@FormParam("comment")
@Size(min = 1, max = 255)
private String comment;
}
@ErrorTemplate(name = "/board_error")
public BoardBean submitMessage(@Valid @BeanParam ContributeBean contribute)
public BoardBean submitMessage( @FormParam("name") @Size(min = 1, max = 255) String name, @FormParam("comment") @Size(min = 1, max = 255) String comment ) {とか、さすがに嫌だろう。それに、検証エラー箇所がわかりにくくなる(後述)
<!DOCTYPE html output>
<html>
<head>
<title>Board</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
ul.error { list-style-type: square; color: red }
</style>
</head>
<body>
<ul class="error">
{{#.}}
<li>
CLASS: {{.}} <br>
PATH: {{path}} <br>
MESSAGE: {{message}} <br>
INVALID VALUE: "{{invalidValue}}"
</li>
{{/.}}
</ul>
<form method="POST" action="submit">
名前 : <input type="text" name="name" value="{{name}}"/>
コメント : <input type="text" name="comment" size="80"/>
<input type="submit"/>
</form>
</body>
</html>
public BoardBean submitMessage( @FormParam("name") @Size(min = 1, max = 255) String name, @FormParam("comment") @Size(min = 1, max = 255) String comment ) {となっていたら、PATH がわかりにくくなる (原因箇所が JAX-RS 側の第何引数かしかわからない) ので、引数は Bean にまとめた方がいい。PATH は次のようになる
投稿者欄、コメント欄を空白のまま [送信] ボタンを押すと、Bean Validation の検証エラーが表示される
@Provider
@Priority(Priorities.USER)
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
@Override
public Response toResponse(final ConstraintViolationException exception) {
return Response.status(Status.BAD_REQUEST)
.entity(new Viewable("/error", exception))
.build();
}
}
}}