接受GET时不接受POST

接受GET时不接受POST

本文介绍了接受GET时不接受POST resquest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从spring控制器中的angularjs进行http调用并发布json数据,但我在angularjs中收到此错误:

  angular.js:10661 POST http:// localhost:8080 / shopng / product / add 404(不是
找到)
(匿名)@ angular.js:10661
sendReq @ angular.js:10480
serverRequest @ angular.js:10187
processQueue @ angular.js:14634
(匿名)@ angular.js:14650
$ eval @ angular.js :15916
$ digest @ angular.js:15727
$ apply @ angular.js:16024
(匿名)@ angular.js:23416
eventHandler @ angular.js:3293
product_service.js:35添加产品时出错
product_controller.js:30 {productName:ads,productPrice:34,description:ads,imageUrl:asd}

但是当我使用GET请求时,它并没有给我任何错误,但它表示不支持媒体类型,当我删除媒体时键入并执行然后在数据库中添加'NULL'值。



这是我的RestController:

  @RestController 
@RequestMapping(/ product)
public class ProductRestController {
@Autowired
ProductDao productDao;
//添加产品

@RequestMapping(value =/ add,method = RequestMethod.POST,consumes =application / json)
public ResponseEntity< Void> createProduct(@ModelAttribute(product)Product product){
System.out.println(Creating Product+ product.getProductName());

if(productDao.isProductExit(product)){
System.out.println(具有名称的产品+ product.getProductName()+已经存在);
返回新的ResponseEntity< Void>(HttpStatus.CONFLICT);
}

productDao.add(product);

返回新的ResponseEntity< Void>(HttpStatus.CREATED);
}

这是我的角度服务请求:

  function addProduct(product){
var deferred = $ q.defer();
$ http.post('http:// localhost:8080 / shopng / product / add',product)
.then(
function(response){
deferred.resolve (response.data);
},
函数(errResponse){
console.log('添加产品时出错');
deferred.reject(errResponse);
}

);
return deferred.promise;
}

这是我项目的github回购:



更新代码


I am trying to make a http call from angularjs in spring controller and post json data but I am getting this error in angularjs :

angular.js:10661 POST http://localhost:8080/shopng/product/add 404 (Not
Found)
(anonymous) @ angular.js:10661
sendReq @ angular.js:10480
serverRequest @ angular.js:10187
processQueue @ angular.js:14634
(anonymous) @ angular.js:14650
$eval @ angular.js:15916
$digest @ angular.js:15727
$apply @ angular.js:16024
(anonymous) @ angular.js:23416
eventHandler @ angular.js:3293
product_service.js:35 Error while adding product
product_controller.js:30 {productName: "ads", productPrice: 34, description: "ads", imageUrl: "asd"}

But when I use GET request then it's not giving me any error but it says media type not supported and when I remove the media type and perform then 'NULL' value is added in database.

Here is my RestController:

@RestController
@RequestMapping("/product")
public class ProductRestController {
@Autowired
ProductDao productDao;
//Add Product

 @RequestMapping(value = "/add", method= RequestMethod.POST, consumes="application/json")
    public ResponseEntity<Void> createProduct(@ModelAttribute("product") Product product) {
        System.out.println("Creating Product " + product.getProductName());

        if (productDao.isProductExit(product)) {
            System.out.println("A Product with name " + product.getProductName() + " already exist");
            return new ResponseEntity<Void>(HttpStatus.CONFLICT);
        }

       productDao.add(product);

       return new ResponseEntity<Void>(HttpStatus.CREATED);
    }

Here is my angular's service making request:

function addProduct(product){
    var deferred = $q.defer();
    $http.post('http://localhost:8080/shopng/product/add', product)
        .then(
           function(response){
               deferred.resolve(response.data);
           },
           function(errResponse){
               console.log('Error while adding product');
               deferred.reject(errResponse);
           }

        );
    return deferred.promise;
 }

here is my github repo of the project : https://github.com/Bk073/ShoppingAny help would be appreciated.

解决方案

I have gone through your securityconfig class.

Please disable the csrf as below.

http.authorizeRequests().antMatchers("/admin/**")
    .access("hasRole('ROLE_ADMIN')").and().formLogin()
    .loginPage("/login").failureUrl("/login?error")
    .usernameParameter("username")
    .passwordParameter("password")
    .and().logout().logoutSuccessUrl("/login?logout")
    .and().csrf().disable();
    //.and().exceptionHandling().accessDeniedPage("/403");

You are getting 404 - because the csrf check is throwing an exception. To handle that exception, you have not added an exception handler. So the 404 means - exception handler is not found, not the post request itself.

If you need to secure with csrf, keep in mind to send csrf token with the request.

Please see the below attachment with successful response.

updated code https://github.com/supun/Shopping

这篇关于接受GET时不接受POST resquest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 22:27