WEB API練習2


  • ディレクトリ構造
  • RoleDao.JAva修正
  • package kr.or.connect.jdbcexam.dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import kr.or.connect.jdbcexam.dto.Role;
    
    public class RoleDao {
    	private static String dburl = "jdbc:mysql://localhost:3306/connectdb?serverTimezone=UTC";
    	private static String dbUser = "connectuser";
    	private static String dbpasswd = "PASSWORD";
        
    	public Role getRole(Integer roleId) {
    		Role role = null;
    		Connection conn = null;
    		PreparedStatement ps = null;
    		ResultSet rs = null;
    		
    		try {
    			Class.forName("com.mysql.cj.jdbc.Driver");
    			conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
                
    			String sql = "SELECT role_id, description FROM role WHERE role_id = ?";
    			ps = conn.prepareStatement(sql);
    			ps.setInt(1, roleId);	// 	arg1 : ?의 순서. arg2 : ?의 값
    			rs = ps.executeQuery();
    			
    			if(rs.next()) {
    				int id = rs.getInt("role_id");
    				String description = rs.getString(2);
    				role = new Role(id, description);
    			}
    		}catch (Exception e) {
    			e.printStackTrace();
    		}finally {
    			if(rs != null) {
    				try {
    					rs.close();					
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    			}
    			if(ps != null) {
    				try {
    					ps.close();					
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    			}
    			if(conn != null) {
    				try {
    					conn.close();					
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    		return role;
    	}
    }
  • RoleByIdServlet.Java-servletファイル
  • を追加
    package kr.or.connect.webapiexam.api;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import kr.or.connect.jdbcexam.dao.RoleDao;
    import kr.or.connect.jdbcexam.dto.Role;
    
    @WebServlet("/roles/*")
    public class RoleByIdServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
          
        public RoleByIdServlet() {
            super();
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        	response.setCharacterEncoding("utf-8");
    		response.setContentType("application/json");
    
    		String pathInfo = request.getPathInfo(); // /roles/{roleId}
    		String[] pathParts = pathInfo.split("/");
    		String idStr = pathParts[1];	// {roleId}에 해당. pathParts[0]는 roles
    		int id = Integer.parseInt(idStr);
    
    		RoleDao dao = new RoleDao();
    
    		Role role = dao.getRole(id);
    
    		ObjectMapper objectMapper = new ObjectMapper();
    		String json = objectMapper.writeValueAsString(role);
    
    		PrintWriter out = response.getWriter();
    		out.println(json);
    		out.close();
    	}
    }
    結果