无法访问 Spring Boot 和 Jersey 应用程序中的某些控制器
Cannot access some controllers in a Spring Boot and Jersey application
我在访问某些控制器时遇到了一个小问题。当我向 /
发送请求时,我得到一个 HTTP 404,movies
路径也是如此。
package com.emo.server;
@Configuration
@ComponentScan({"com.emo.server", "com.emo.server.controller"})
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Path("/")
public String home() {
return "Hello World";
}
@Configuration
@EnableResourceServer
protected static class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests().antMatchers("/movies").permitAll()
.and()
.authorizeRequests()
.antMatchers("/test")
.access("#oauth2.hasScope('read')");
// @formatter:on
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("test");
}
}
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// @formatter:off
clients.inMemory()
.withClient("my-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.resourceIds("test")
.accessTokenValiditySeconds(60)
.and()
.withClient("my-client-with-secret")
.authorizedGrantTypes("client_credentials", "password")
.authorities("ROLE_CLIENT")
.scopes("read")
.resourceIds("test")
.secret("secret");
// @formatter:on
}
}
MovieController 的一部分:
package com.emo.server.controller;
@Component
@Path("/movies")
@Produces(MediaType.APPLICATION_JSON)
public class MovieController {
private static final Logger LOGGER = LoggerFactory.getLogger(MovieController.class);
@GET
@Path("/")
public Response getMovies() {
System.out.println("Just a test after ....");
}
从这个开始,我希望之后能得到一个测试,但我得到了 404。
控制台部分:
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 404 Not Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< X-Application-Context: application
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 07 Oct 2015 19:15:20 GMT
<
* Connection #0 to host localhost left intact
{"timestamp":1444245320491,"status":404,"error":"Not Found","message":"No message available","path":"/"}
我的项目图片:
我尝试用 resourceConfig
注册我的控制器,但发生了同样的事情。
我可以访问一些端点,例如 oauth/token
、health
、...我使用 spring-boot 和 jersey 作为 REST 框架 java 8。
尝试将您的控制器注释更改为如下内容:
@Controller
@RequestMapping(value="/movies", produces="application/json)
public class MovieController{
@RequestMapping(value="/", method=RequestMethod.GET)
public MyResponseObject getMovies(){
//you can return a POJO object if you are using a Jackson Mapper
return new MyResponseObject("foo", "bar");
}
}
我在访问某些控制器时遇到了一个小问题。当我向 /
发送请求时,我得到一个 HTTP 404,movies
路径也是如此。
package com.emo.server;
@Configuration
@ComponentScan({"com.emo.server", "com.emo.server.controller"})
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Path("/")
public String home() {
return "Hello World";
}
@Configuration
@EnableResourceServer
protected static class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests().antMatchers("/movies").permitAll()
.and()
.authorizeRequests()
.antMatchers("/test")
.access("#oauth2.hasScope('read')");
// @formatter:on
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("test");
}
}
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// @formatter:off
clients.inMemory()
.withClient("my-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.resourceIds("test")
.accessTokenValiditySeconds(60)
.and()
.withClient("my-client-with-secret")
.authorizedGrantTypes("client_credentials", "password")
.authorities("ROLE_CLIENT")
.scopes("read")
.resourceIds("test")
.secret("secret");
// @formatter:on
}
}
MovieController 的一部分:
package com.emo.server.controller;
@Component
@Path("/movies")
@Produces(MediaType.APPLICATION_JSON)
public class MovieController {
private static final Logger LOGGER = LoggerFactory.getLogger(MovieController.class);
@GET
@Path("/")
public Response getMovies() {
System.out.println("Just a test after ....");
}
从这个开始,我希望之后能得到一个测试,但我得到了 404。
控制台部分:
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 404 Not Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< X-Application-Context: application
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 07 Oct 2015 19:15:20 GMT
<
* Connection #0 to host localhost left intact
{"timestamp":1444245320491,"status":404,"error":"Not Found","message":"No message available","path":"/"}
我的项目图片:
我尝试用 resourceConfig
注册我的控制器,但发生了同样的事情。
我可以访问一些端点,例如 oauth/token
、health
、...我使用 spring-boot 和 jersey 作为 REST 框架 java 8。
尝试将您的控制器注释更改为如下内容:
@Controller
@RequestMapping(value="/movies", produces="application/json)
public class MovieController{
@RequestMapping(value="/", method=RequestMethod.GET)
public MyResponseObject getMovies(){
//you can return a POJO object if you are using a Jackson Mapper
return new MyResponseObject("foo", "bar");
}
}