请求方法' POST'春季靴子不支持

时间:2015-09-02 05:51:09

标签: java mysql spring spring-boot

我正在Spring Boot Billing中开发一个项目。我已成功构建并运行此项目,但存在一些问题。

当我尝试通过POST方法插入数据但浏览器显示不支持POST方法时。

这是我的控制器       BillingController.java

 package com.billing.controller;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.billing.model.Restaurant_billing;
import com.billing.model.Restaurant_billingDao;
import com.billing.model.billing;
import com.billing.model.billingDao;
import com.billing.model.itemDao;
import com.billing.model.tax_billing;
import com.billing.model.tax_billingDao;


@Controller
@RestController
@RequestMapping("/restaurant")
public class BillingController {

@Autowired
private itemDao itemDao;

@Autowired
private billingDao billingDao;

@Autowired
private Restaurant_billingDao restaurant_billingDao;

@Autowired
private tax_billingDao tax_billingDao;

SessionFactory sessionFactory;

Session sesion=null;
org.hibernate.Transaction tx=null;

   @RequestMapping(value="/create", method = RequestMethod.POST, consumes =   
   MediaType.APPLICATION_JSON_VALUE)    
    @ResponseBody 
 public String R_billing(@RequestBody final Restaurant_billing r_billing[] ,HttpServletResponse response,HttpServletRequest request)
 throws ServletException { 

  try{

      billing billingObject = new billing();
      billingDao.save(billingObject);
      int billing_id = billingObject.getId();

      tax_billing tax_billing= new tax_billing();

      tax_billing.setBilling_id(billing_id);
      tax_billing.setTax_amount("140");
      tax_billingDao.save(tax_billing);      


    for(Restaurant_billing prof:r_billing){

    prof.setBilling_id(billing_id); 
    restaurant_billingDao.save(prof);

   }
     }
    catch(Exception ex){
    return "Error creating the user: " + ex.toString();
       }
    return ("User profession added successfully");  
     }


   }

这是我的模型类

Restaurant_billing.java

package com.billing.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name ="billing_restaurant")
public class Restaurant_billing {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id; 

@Column(name="billing_id")
private int billing_id;

@Column(name="itemid")
private String itmeid;

@Column(name="quantity")
private String quantity;

@Column(name="billing_type")
private String billing_type;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getBilling_id() {
    return billing_id;
}

public void setBilling_id(int billing_id) {
    this.billing_id = billing_id;
}

public String getItmeid() {
    return itmeid;
}

public void setItmeid(String itmeid) {
    this.itmeid = itmeid;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getBilling_type() {
    return billing_type;
}

public void setBilling_type(String billing_type) {
    this.billing_type = billing_type;
}



}

1 个答案:

答案 0 :(得分:0)

不需要使用@Controller仅使用@RestController并且还分配sesion和tx值。 之后,您只需尝试以下代码,



@RequestMapping(value = "/create", method = RequestMethod.POST)

public @RequestBody Restaurant_billing R_billing(final HttpServletResponse response){ 

  try{

      billing billingObject = new billing();

     //Here set the billingObject values

      billingDao.save(billingObject);

      int billing_id = billingObject.getId();

      tax_billing tax_billing= new tax_billing();

      tax_billing.setBilling_id(billing_id);
      tax_billing.setTax_amount("140");
      tax_billingDao.save(tax_billing);      


    for(Restaurant_billing prof:r_billing){

    prof.setBilling_id(billing_id); 
    restaurant_billingDao.save(prof);

   }
     }
    catch(Exception ex){
    return "Error creating the user: " + ex.toString();
       }
    return ("User profession added successfully");  
     }
//here return obj

   }