我正在尝试使用 Dropwizard 创建一个 RESTful Web API。我相信我的数据库已连接并正常运行。但是,每当我尝试对特定项目执行 GET 请求时,都会遇到错误消息:
ERROR [2015-07-08 21:55:23,867]
io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a
request: 084335cfa9b38151
! groovy.lang.MissingMethodException: No signature of method: static
edu.oregonstate.mist.catalogapitest.db.CourseDAO.findByCrn() is
applicable for argument types: (java.lang.Integer) values: [12345]
不完全确定,但我认为这可能是 Jetty 问题而不是 Dropwizard 问题。
下面我包含了我认为可能存在问题的代码。
CourseDAO.groovy
package edu.oregonstate.mist.catalogapitest.db
import edu.oregonstate.mist.catalogapitest.core.Course
import edu.oregonstate.mist.catalogapitest.mapper.CourseMapper
import org.skife.jdbi.v2.sqlobject.SqlUpdate
import org.skife.jdbi.v2.sqlobject.SqlQuery
import org.skife.jdbi.v2.sqlobject.Bind
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper
@RegisterMapper(CourseMapper)
public interface CourseDAO extends Closeable {
@SqlQuery("""
SELECT *
FROM COURSES
WHERE CRN = :crn
""")
List<Course> findByCrn(@Bind("crn") Integer crn)
@SqlQuery("""
SELECT *
FROM COURSES
WHERE COURSENAME = :courseName
""")
List<Course> findByCourseName(@Bind("courseName") String courseName)
void close()
}
CourseResource.groovy
package edu.oregonstate.mist.catalogapitest.resources
import edu.oregonstate.mist.catalogapitest.core.Course
import edu.oregonstate.mist.catalogapitest.db.CourseDAO
import io.dropwizard.jersey.params.IntParam
import com.google.common.base.Optional
import javassist.NotFoundException
import org.eclipse.jetty.server.Response
import javax.validation.constraints.NotNull
import javax.ws.rs.Consumes
import javax.ws.rs.GET
import javax.ws.rs.POST
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
import javax.ws.rs.QueryParam
import javax.ws.rs.WebApplicationException
import javax.ws.rs.core.MediaType
@Path("/course")
@Produces(MediaType.APPLICATION_JSON)
class CourseResource {
private final CourseDAO courseDAO
public CourseResource(CourseDAO courseDAO) {
this.courseDAO = courseDAO
}
@GET
@Path('{crn}')
public List<Course> getByCrn(@PathParam('crn') IntParam crn) {
println(crn.get())
final List<Course> courses = CourseDAO.findByCrn(crn.get())
if (courses.isEmpty()) {
throw new WebApplicationException(404)
}
return courses
}
@GET
@Path('/name/{courseName}')
public List<Course> getByCourseName(@PathParam('courseName') String courseName) {
println(courseName)
final List<Course> courses = CourseDAO.findByCourseName(courseName)
if (courses.isEmpty()) {
throw new WebApplicationException(404)
}
return courses
}
}
最后,这是我的仓库的链接以供完整参考:https ://github.com/osu-mist/catalog-api-test
谢谢!