服务器响应404 - POST请求

时间:2018-03-26 15:50:33

标签: java spring reactjs rest

我希望创建一个将用户输入保存到数据库的Web应用程序。 我正在使用Java和React作为用户界面,但我一直收到404错误。

我有以下脚本:

阵营:

addCreditCard(event) {
var that = this;
event.preventDefault();
let card_data = {
    cardholder : this.refs.cardholder.value,
    cardnumber : this.refs.cardnumber.value,
    card_identifier : (this.refs.cardnumber.value).substr(15),
    expiration : this.refs.expiration.value,
    cvc : this.refs.cvc.value
};
console.log('Ez itt: ' + JSON.stringify(card_data))
const request = {
  method: 'post',
  headers: {
    'Accept': 'application/json, text/plain, */*',
    'Content-Type': 'application/json'
  },
     body: JSON.stringify(card_data)

}
let creditcards = that.state.creditcards;
      creditcards.push(card_data);
        that.setState({
         creditcards : creditcards
})

console.log(creditcards)

fetch('/api/new-card', request)
.then(response => {
    if (response.ok) {
         return response.json();
       } else {
          throw new Error('Something went wrong ...');
       }
     })
       .then(data => this.setState({ creditcards: data.creditcards }))
       .catch(error => this.setState({ error }))
       }

爪哇

@Path("")
@Produces(ExtendedMediaType.APPLICATION_JSON_UTF8)
@Consumes(ExtendedMediaType.APPLICATION_JSON_UTF8)
public class CreditCardRest {

/**
 * Injected configurationDao.
 */
@Inject
@Named(SessionFactoryProducer.SQL_SESSION_FACTORY)
private CardDAO cardDAO;

@RequestMapping(value = "/new-card", method = RequestMethod.POST)
@Transactional
public Response.ResponseBuilder saveCreditCardData(@PathParam("cardholder") final String cardholder,
        @PathParam("cardnumber") final Integer cardnumber,
        @PathParam("expiration") final String expiration,
        @PathParam("cvc") final Integer cvc,
        @PathParam("card_identifier") final Integer card_identifier,
        @Context HttpServletResponse servletResponse) throws Exception {

    Reader reader = Resources.getResourceAsReader("mybatis-card-service.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession session = sqlSessionFactory.openSession();

    //Create a credit card object
    cardDAO.saveCreditCardData(cardholder, cardnumber, expiration, cvc, card_identifier);

    System.out.println("record inserted successfully");
    session.commit();
    session.close();

    return Response.status(200);
   }
  }

JS代码有效,问题出在Java类和Ract之间的连接处...... 对不起,但是我无法弄明白......对于cource来说,这只是整个代码的一部分,但我希望对于已经在第一眼看到的人来说可能是显而易见的......非常感谢!

...谢谢

1 个答案:

答案 0 :(得分:2)

没有/api路径。您/new-card端点的路径为/new-card,因为class级别没有根路径。

@Path("/api")
@Produces(ExtendedMediaType.APPLICATION_JSON_UTF8)
@Consumes(ExtendedMediaType.APPLICATION_JSON_UTF8)
public class CreditCardRest {

}