Hello friends I need help I'm trying to make a rest api but I do not get it in java, I know it's all this long but I need some help since it's for a University Project. Thanks This is my code: bean:
package bean;
public class Album {
private String idalbum;
private String nombrealbum;
private String fotoalbum;
private String nombreartista;
public Album(){
}
public Album(String idalbum, String nombrealbum, String fotoalbum, String nombreartista) {
super();
this.idalbum = idalbum;
this.nombrealbum = nombrealbum;
this.fotoalbum = fotoalbum;
this.nombreartista = nombreartista;
}
public String getIdalbum() {
return idalbum;
}
public void setIdalbum(String idalbum) {
this.idalbum = idalbum;
}
public String getNombrealbum() {
return nombrealbum;
}
public void setNombrealbum(String nombrealbum) {
this.nombrealbum = nombrealbum;
}
public String getFotoalbum() {
return fotoalbum;
}
public void setFotoalbum(String fotoalbum) {
this.fotoalbum = fotoalbum;
}
public String getNombreartista() {
return nombreartista;
}
public void setNombreartista(String nombreartista) {
this.nombreartista = nombreartista;
}
}
DAO:
package dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import edu.util.MySQLConexion;
import bean.Album;
public class AlbumDAO {
private MySQLConexion sql;
public AlbumDAO(){
this.sql = new MySQLConexion();
}
public List<Album> listadoAlbum(){
List<Album> lista= new ArrayList<Album>();
Connection cn = sql.getConnection();
try{
CallableStatement cs = cn.prepareCall("{call sp_album()}");
ResultSet rs = cs.executeQuery();
while(rs.next()){
Album album = new Album();
album.setIdalbum(rs.getString("idalbum"));
album.setNombrealbum(rs.getString("nombrealbum"));
album.setFotoalbum(rs.getString("fotoalbum"));
album.setNombreartista(rs.getString("nombreartista"));
lista.add(album);
}
rs.close();
cs.close();
cn.close();
}catch (Exception e) {
// TODO: handle exception
}
System.out.println(lista);
return lista;
}
}
This is my connection
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLConexion {
public Connection getConnection() {
Connection cn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
cn= DriverManager.getConnection("jdbc:mysql://localhost:3306/ProyectoMusica", "root", "mysql");
} catch (SQLException e) {
// TODO: handle exception
} catch (Exception e) {
// TODO: handle exception
}
return cn;
}
}
Finally this is my Rest service
package rest;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import bean.Album;
import dao.AlbumDAO;
@Path("servicioAlbum")
public class ServicioAlbum {
@Path("listarAlbum")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Album> listarAlbum(){
AlbumDAO albumdoDao = new AlbumDAO();
return albumdoDao.listadoAlbum();
}
}
And this is my xml configuration
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServidorProyecto</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Servicio REST Basico</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Servicio REST Basico</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>