我尝试制作自己的Java J2EE DAO。它一直给我404错误。我还尝试发送GET请求,再次出现404错误。我有所有正确的导入,web.xml,pom.xml等。我检查了那些。
我试图只获得" ingredientnaam"来自url / restservices / ingredients的DAO变量。使用Eclipse,有0个错误。
有人知道我可以编辑的功能DAO以适应我的DAO吗?或者有人看到我的代码中有任何错误吗?非常感谢。
IngredientResource.java
@Path("/ingredients")
public class IngredientResource {
@GET
@Produces("application/json")
public String getIngredients() {
IngredientService service = ServiceProvider.getIngredientService();
JsonArray ingredientArray = buildJsonCountryArray(service.getAllIngredients());
System.out.println(service.getAllIngredients());
return ingredientArray.toString();
}
private JsonArray buildJsonCountryArray(List<Ingredient> ingredients) {
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
for (Ingredient c : ingredients) {
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("ingredientnaam", c.getIngredientnaam());
jsonArrayBuilder.add(job);
}
return jsonArrayBuilder.build();
}
}
IngredientDAO.java
public class IngredientDAO extends BaseDAO {
private IngredientDAO ingredientDAO = new IngredientDAO();
private List<Ingredient> selectIngredients(String query) {
List<Ingredient> results = new ArrayList<Ingredient>();
try (Connection con = super.getConnection()) {
Statement stmt = con.createStatement();
ResultSet dbResultSet = stmt.executeQuery(query);
while (dbResultSet.next()) {
String ingredientnaam = dbResultSet.getString("ingredientnaam");
Ingredient ingredient = ingredientDAO.findByString(ingredientnaam);
Ingredient newIngredient = new Ingredient(ingredientnaam, 1, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0);
results.add(newIngredient);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return results;
}
public List<Ingredient> findAll() {
return selectIngredients("SELECT ingredientId, name, account, FK_ingredientID FROM ingredient");
}
public Ingredient findByString(String ingredientId) {
return selectIngredients("SELECT ingredientnaam FROM ingredient WHERE ingredientnaam = " +ingredientId).get(0);
}
}
IngredientService.java
package voedseldagboek.dagboek.domain;
import java.util.List;
import voedseldagboek.dagboek.persistence.*;
public class IngredientService {
private IngredientDAO ingredientDAO = new IngredientDAO();
public List<Ingredient> getAllIngredients() {
return ingredientDAO.findAll();
}
}
ServiceProvider.java
package voedseldagboek.dagboek.domain;
public class ServiceProvider {
private static IngredientService ingredientService = new IngredientService();
public static IngredientService getIngredientService() {
return ingredientService;
}
}