java.lang.IllegalArgumentException:目标对象不能为null JAX-RS

时间:2017-06-07 10:03:13

标签: java angularjs jax-rs

我正在使用JAX-RS,AngularJS,Spring Boot和Spring MVC开发Web应用程序。

我希望在我的数据库中添加一辆必须独一无二的汽车。所以我这样做了:

  • Cars.java:

    @Entity  @Table(name = "idCars",uniqueConstraints = @UniqueConstraint(columnNames = {"name", "idAidCars"}))
        public class Actifs implements Serializable {
    
            @Id
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            @Column(name="idCars")
            private long idCars;
    
            @Column(name="Name") 
            private String Name;
        }
    
  • CarsRestController.java:

@CrossOrigin("*")
@RestController
@RequestMapping(value="/Cars")
public class CarsRestController {

// Add car in DB
@RequestMapping(value="/Add",method = {RequestMethod.POST, RequestMethod.GET})
  public Cars AddCars(@RequestBody(required=false) @Valid Cars cars){
      return repository.save(cars); // object is NULL
  }

}
  • AngularJS:
$scope.AddCars=function(){
 var dataObj = {
                  name :  $scope.name  
          };  
  console.log("Name of car "+$scope.name) // BMW

var res = $http.post("/Cars/Add", dataObj);
res.success(function(data, status, headers, config) {
    $scope.message = data;
    console.log("** "+JSON.stringify(data)); // Nothing displayed
});
res.error(function(data, status, headers, config) {
    alert( "failure message: " + JSON.stringify({data: data}));
  });     
$scope.name='';
};
  • Html表格:
<form role="form" enctype="multipart/form-data" name="myForm" >
<label for="name">Name Cars *:</label> 
<input type="text" ng-model="name" required="required">   
<button type="submit" ng-click="AddCars()"> Add Cars </button>
</form>

在附加测试后,我总是将从angularJS发送到API的对象总是为NULL,我不知道为什么?

  

目标对象不得为null;嵌套异常是   java.lang.IllegalArgumentException:目标对象不能为空

如何解决这个问题?

  • WebSecurityConfig

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(securedEnabled=true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
                @Autowired
                  protected void globalConfig(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
                 //auth.inMemoryAuthentication().withUser("user").password("123").roles("USER");
                     auth.jdbcAuthentication()
                         .dataSource(dataSource)
                         .passwordEncoder(passwordEncoder())
                         .usersByUsernameQuery("select username as principal, password as credentials, etat as actived from utilisateurs where username=?")
                         .authoritiesByUsernameQuery("select u.username as principal, ur.nom_role as role from utilisateurs u inner join roles ur on(u.roles_id=ur.id_role) where u.username=?")
                         .rolePrefix("ROLE_");
                 }
            protected void configure(HttpSecurity http) throws Exception {
    
            http
                      .sessionManagement().maximumSessions(100).maxSessionsPreventsLogin(false).expiredUrl("/Login");
                      http
                       .authorizeRequests()
                       .antMatchers("/images/**","/pdf/**","/Template/**","/Views/**","/MainApp.js","/css/**", "/js/**").permitAll()
                       .antMatchers("/Cars/**").access("hasRole('ADMIN')")
            .antMatchers("/Login").anonymous()
                       .anyRequest().authenticated()
                       .and()
                       .exceptionHandling().accessDeniedPage("/403")
                       .and()
                       .formLogin().loginPage("/Login").permitAll()
                       .defaultSuccessUrl("/", true)  
                       .failureUrl("/Login?error=true")
                           .and()
                           .csrf()
                           .and()
                            .rememberMe().tokenRepository(persistentTokenRepository())
                            .tokenValiditySeconds(360000); 
            }
    

    谢谢,

0 个答案:

没有答案