はじめに

環境設定

JAX-RS Resource

https://github.com/kagyuu/JerseyMVCExam/blob/master/src/main/java/com/mycompany/jerseymvcexam/BoardResource2.java

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;
    }
}

https://github.com/kagyuu/JerseyMVCExam/blob/master/src/main/java/com/mycompany/jerseymvcexam/ContributeBean.java

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;
}

エラーページ

https://github.com/kagyuu/JerseyMVCExam/blob/master/src/main/webapp/board_error.mustache

<!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>

実行結果

screenshot.png

投稿者欄、コメント欄を空白のまま [送信] ボタンを押すと、Bean Validation の検証エラーが表示される

検証エラーを共通化したければ ExceptionMapper? を使う

@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();
  }
}

Java#Glassfish

}}


添付ファイル: filescreenshot.png 1982件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2015-07-25 (土) 10:19:11 (3191d)
Short-URL: http://hondou.homedns.org/pukiwiki/index.php?cmd=s&k=a0a149c4f7
ISBN10
ISBN13
9784061426061