gpt4 book ai didi

java - 状态 404 – 在使用 RESTFul API 的 @post 注释上未找到

转载 作者:行者123 更新时间:2023-12-02 01:40:22 24 4
gpt4 key购买 nike

我已经在 java 文件中给出了配置详细信息,当我尝试访问 URL 时,出现状态 404 错误。找不到我哪里出错了。我尝试访问的 URL 是 http://localhost:8080/foodCourt/webapi/foods。请帮忙解决代码。在此我分享代码详细信息。当我使用 @Post 注释时出现此错误。请查找 web.xml 文件的详细信息。我在 url 模式的文件中提供了 webapi。我提供了必要的 jar 文件,其中包括 hibernate、jersey、jboss

@ApplicationPath("webapi")
public class AplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(resources.Foods.class);
}
}

@XmlRootElement
public class FoodCourt {
private int foodCourtId;
private String foodCourtName;
private int seatingCapacity;
private int votes;
public int getFoodCourtId() {
return foodCourtId;
}
public void setFoodCourtId(int foodCourtId) {
this.foodCourtId = foodCourtId;
}
public String getFoodCourtName() {
return foodCourtName;
}
public void setFoodCourtName(String foodCourtName) {
this.foodCourtName = foodCourtName;
}
public int getSeatingCapacity() {
return seatingCapacity;
}
public void setSeatingCapacity(int seatingCapacity) {
this.seatingCapacity = seatingCapacity;
}
public int getVotes() {
return votes;
}
public void setVotes(int votes) {
this.votes = votes;
}
}


@Path("/foods")
public class Foods {
private Foods()
{}
private FoodDAO fooddao = new FoodCourtDAOImple();
@POST
@Consumes("application/json")
@Produces("application/xml,application/json")
public Response fooddetails(FoodCourt foodDTO) throws Exception
{
int id=fooddao.addFoodDetails(foodDTO);
System.out.println(id);
return Response.status(201).entity(id).build();
}
}

@Entity
@Table(name="FoodCourt")
public class FoodCourtEntity {
@Id
private int foodCourtId;
private String foodCourtName;
private int seatingCapacity;
private int votes;
public int getFoodCourtId() {
return foodCourtId;
}
public void setFoodCourtId(int foodCourtId) {
this.foodCourtId = foodCourtId;
}
public String getFoodCourtName() {
return foodCourtName;
}
public void setFoodCourtName(String foodCourtName) {
this.foodCourtName = foodCourtName;
}
public int getSeatingCapacity() {
return seatingCapacity;
}
public void setSeatingCapacity(int seatingCapacity) {
this.seatingCapacity = seatingCapacity;
}
public int getVotes() {
return votes;
}
public void setVotes(int votes) {
this.votes = votes;
}
}

public class FoodCourtDAOImple implements FoodDAO {
Session session;
SessionFactory factory;
@Override
public int addFoodDetails(FoodCourt foodcourt) throws Exception {
// TODO Auto-generated method stub
int foodCourtId=0;
try
{
factory = HibernateUtil.getSessionFactory();
session = factory.openSession();
Transaction tx = session.beginTransaction();
FoodCourt foodDTO=foodcourt;
FoodCourtEntity foodEntity=new FoodCourtEntity();
foodEntity.setFoodCourtId(foodDTO.getFoodCourtId());
foodEntity.setFoodCourtName(foodDTO.getFoodCourtName());
foodEntity.setSeatingCapacity(foodDTO.getSeatingCapacity());
foodEntity.setVotes(foodDTO.getVotes());
foodCourtId=(int) session.save(foodEntity);
return foodCourtId;
}catch (HibernateException exception) {
throw new Exception(exception.getMessage());
}
finally {
if (session != null || session.isOpen()) {
session.close();
}
}
}
}


public class HibernateUtil {
private static SessionFactory factory;
private HibernateUtil() {
}
public static SessionFactory getSessionFactory() {

Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry;
serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
factory = cfg.buildSessionFactory(serviceRegistry);
return factory;
}
}


<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.aarthi.rest.messenger</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

最佳答案

我看不到 Controller 的任何映射,请尝试使用以下网址:

 http://localhost:8080/webapi/foods

关于java - 状态 404 – 在使用 RESTFul API 的 @post 注释上未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com