没有消息可用“Spring Boot,JAX-RS和angularJS出错

时间:2017-05-08 15:56:50

标签: java angularjs spring jsp

我创建了项目Spring Boot项目并实现了Restful服务,如下所示:

@CrossOrigin
@RestController
@RequestMapping("/Users")
public class UsersRestController {

@RequestMapping(value="/AddUser")
public Users addUsers( Users user){
    return UM.saveT(user);
}
}

AngularJS方法:

Var url ="localhost:8080/App/Users";

$scope.saveUsers = function() {
$http.post(url+"/AddUser?name="+$scope.NameUser+"&email="+$scope.emailUser)
      .success(function(data) {
                $scope.ListUsers = data;
                $window.location.href="localhost:8080/App/AllUsers";
              }).error(function(err, data) {
                $scope.messag = "Add user error !!";
              });
}

在执行添加用户的操作之前,管理员必须进行身份验证才能访问我的应用程序。我做了这个配置:

@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)
             .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("/Users/").access("hasRole('ADMIN')")  
      .antMatchers("/Login").anonymous()
      .anyRequest().authenticated()
      .and()
      .formLogin().loginPage("/Login").permitAll()
      .defaultSuccessUrl("/")
      .failureUrl("/Login?error=true")  
       .and()
         .logout()
            .invalidateHttpSession(true)
            .clearAuthentication(true)
            .logoutUrl("/logout")
            .permitAll() 
           .logoutSuccessUrl("/Login")
        .and().exceptionHandling().accessDeniedPage("/403")
        .and()
         .csrf();
}

-class MvcConfig.java:

@Configuration
public class MvcConfig  extends WebMvcConfigurerAdapter{
@Override
public void configureDefaultServletHandling(
     DefaultServletHandlerConfigurer configurer) {
       configurer.enable();
  }
}

我将JSP框架用于网页,因此我无视控制器返回JSP页面:

@Controller
public class WebController {

    @RequestMapping( value = "/Login" )
    public String login(){
    return "login";
    }

    @RequestMapping( value = "/" )
    public String Home(){
    return "index";
    }

    @RequestMapping( value = "/Users" )
    public String Users(){
    return "indexUsers";
    }

- 验证用户后我拥有所有用户列表,但是当你添加新用户时我遇到了这个问题:

  

错误:“找不到”   消息:“没有消息可用”   路径: “/应用/用户/ ADDUSER”   状态:404时间戳:1494256474299

这里是请求的内容:

Request URL:http://localhost:8080/App/Users/AddUser?name=Michel&email=michel@gmail.com  

Request Method:POST

Status Code:404 

Remote Address:[::1]:8080

Referrer Policy:no-referrer-when-downgrade

**Request Headers

Cache-Control:no-cache, no-store, max-age=0, must-revalidate

Content-Type:application/json;charset=UTF-8

Date:Mon, 08 May 2017 15:14:34 GMT

Expires:0

Pragma:no-cache

Transfer-Encoding:chunked

X-Content-Type-Options:nosniff

X-Frame-Options:DENY

X-XSS-Protection:1; mode=block

Accept:application/json, text/plain, */*

Accept-Encoding:gzip, deflate, br

Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4

AlexaToolbar-ALX_NS_PH:AlexaToolbar/alx-4.0.1

Connection:keep-alive

Content-Length:0

Cookie:JSESSIONID=671F3685C71CDEB58726FD1392389FA2

Host:localhost:8080

Origin:http://localhost:8080

Referer:http://localhost:8080/App/Users

User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/57.0.2987.133 Safari/537.36

**Query Strings Parameters:

name=Michel

email=michel@gmail.com

但是当我进行Api Rest(http:// localhost:8080 / Users / AddUser?Name = Michel& email=michel@gmail.com)时,用户将被添加到数据库中。

有人能帮我找到遗漏点吗?

1 个答案:

答案 0 :(得分:0)

默认情况下使用所有Jax-RS方法**获取**。然后我们必须在方法angularjs中定义方法Get。

$scope.saveUsers = function() {
    $http.get("URL_PATH")
          .success(function(data) {

            //Your code here
               })
    }

-Method angularJS:

expect(foo).to match_array(bar)