状态404 –使用RESTFul API在@post注释上找不到

时间:2019-02-05 06:16:51

标签: java spring rest spring-boot spring-mvc

我在java文件中提供了配置详细信息,当我尝试访问URL时,出现状态404错误。 找不到我要去哪里。我尝试访问的URL是http://localhost:8080/foodCourt/webapi/foods。 请帮助解决该代码。 我在此分享代码详细信息。使用@Post注释时出现此错误。请查找web.xml文件的详细信息。我在网址格式文件中提供了webapi。我提供了必要的jar文件,其中包括休眠,球衣,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>

2 个答案:

答案 0 :(得分:2)

我看不到控制器的任何映射,请尝试使用以下URL:

 http://localhost:8080/webapi/foods

答案 1 :(得分:0)

通过将@ApplicationPath(“ webapi”)更改为@ApplicationPath(“ / webapi”)来尝试一次