【问题标题】:No message available” error with Spring Boot , JAX-RS and angularJSSpring Boot、JAX-RS 和 angularJS 出现“无可用消息”错误
【发布时间】:2017-05-08 15:56:50
【问题描述】:

我已经创建了项目 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();
}

-类 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";
    }

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

错误:“未找到” 消息:“没有可用的消息” 路径:“/应用程序/用户/添加用户” 状态: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) 时,用户将被添加到数据库中。

谁能帮我找到这里的缺失点?

【问题讨论】:

  • 在 Angular 方法中,提到的 URL 是 url ="localhost:8080/App/Users"。你说工作正常的那个是http://localhost:8080/Users/AddUser? Name=Michel&email=michel@gmail.com。如果是这样,请从 Angular 基本 URL 中删除 App
  • 对不起@Pete先生,基本网址是http://localhost:8080/App
  • 你是如何在外部测试 API 的?
  • 我在浏览器的地址栏中放了如下url,**localhost:8080/Users/AddUser? Name=Michel&email=michel@gmail.com**,然后我有包含我添加的用户的用户列表(Michel)
  • 浏览器的地址栏发送 HTTP GET 请求而不是 POST。我看到你的请求映射@RequestMapping(value="/AddUser") 它将映射/AddUser上的所有请求方法

标签: java angularjs spring jsp


【解决方案1】:

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

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

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

-方法angularJS:

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

            //Your code here
               })
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多