使用ResponseEntity没有返回值的结果

时间:2016-07-21 21:57:33

标签: jquery spring spring-restcontroller

我使用弹簧靴,它是我的其他控制器之一

@RequestMapping(value = "/members/{memberId}/creditcard", method = RequestMethod.PUT)
public ResponseEntity saveCreditCard(@PathVariable("memberId") Long memberId, @RequestBody CreditCardInfoDto creditCardInfo) throws CreditCardException, ResourceNotFoundException {
    paymentService.saveCreditCard(creditCardInfo);
    return new ResponseEntity(HttpStatus.OK);
}

在客户端,我做

jQuery.ajax({
  type: type,
  url: url,
  contentType: "application/json",
  dataType: "json",
  data: data,
  success: function(data, status, jqXHR) {
    if (data) {
    ...
    }
  },
  error: function(jqXHR, status) {
    ...
  }
});

我总是通过错误部分...结果它

qXHR = Object {readyState: 4, responseText: "", status: 200, statusText: "OK"}, status = "parsererror

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你快到了。在ResponseEntity中,传递第一个参数作为保存卡的结果。 这是示例

@RestController
public class CreditCardController {

    @Autowired PaymentService paymentService;

    @RequestMapping(value = "/members/{memberId}/creditcard", method = RequestMethod.PUT)
    public ResponseEntity saveCreditCard(@PathVariable("memberId") Long memberId, @RequestBody CreditCardInfoDto creditCardInfo)  {
        CreditCardInfoDto savedCardDto =  paymentService.saveCreditCard(creditCardInfo);
        return new ResponseEntity(savedCardDto, HttpStatus.OK);
    }

}


@Service
class PaymentService {
    public CreditCardInfoDto saveCreditCard(CreditCardInfoDto dto) {
        // business logic to save the credit card

        CreditCardInfoDto responseDto = new CreditCardInfoDto();
        responseDto.setName(dto.getName()); //saved name
        responseDto.setCardNumber("******4444"); // get last 4 digit of card
        return responseDto;
    }
}

class CreditCardInfoDto {
    private String name;
    private String cardNumber;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
}

enter image description here