Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource handlers support #15

Closed
j-barata opened this issue Jul 27, 2020 · 10 comments
Closed

Resource handlers support #15

j-barata opened this issue Jul 27, 2020 · 10 comments

Comments

@j-barata
Copy link
Contributor

j-barata commented Jul 27, 2020

Hello,

I'm trying to setup a resource provider within a plugin (the resources shall be provided by the plugin itself), and tried the following code :

@configuration
@EnableWebMvc
public class ImageProvider implements WebMvcConfigurer {
@OverRide
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/icons/*").addResourceLocations("classpath:/icons/");
}
}

But I only get a 404 Error.

Is this kind of feature currently supported ?
Thanks

@hank-cp
Copy link
Owner

hank-cp commented Jul 28, 2020

SBP supports resource loading. Please check out this document that explain how sbp load resources.

and here are related the test cases:

@j-barata
Copy link
Contributor Author

j-barata commented Jul 28, 2020

Thanks for your answer.

The idea was to expose the plugins resources from the application, through the standard WebMvcConfigurer interface.
But when I set the @EnableWebMvc annotation on the application class I get the following error :

java.lang.ClassCastException: class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping cannot be cast to class org.laxture.sbp.internal.PluginRequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping and org.laxture.sbp.internal.PluginRequestMappingHandlerMapping are in unnamed module of loader 'app')

hank-cp added a commit that referenced this issue Jul 29, 2020
@hank-cp
Copy link
Owner

hank-cp commented Jul 29, 2020

static resource handing is added.

Please check out this test case for details:

this feature is available at v0.1.6 and SNAPSHOOT.

@j-barata
Copy link
Contributor Author

j-barata commented Jul 29, 2020

Hi,
Thank you very much for this (very) quick enhancement !

I'm trying to download the new version from maven central but it doesn't seem to be published yet (I tried with 0.1.6 v0.1.6 -SNAPSHOT -SNAPSHOOT but it doesn't know this version).

Here is my maven configuration:

    <dependency>
        <groupId>org.laxture</groupId>
        <artifactId>sbp-spring-boot-starter</artifactId>
        <version>0.1.6</version>
    </dependency>

@hank-cp
Copy link
Owner

hank-cp commented Jul 29, 2020

sorry, fotget to publish...should be available now.

You could also use SNAPSHOT version that always map to latest of matser.

    <dependency>
        <groupId>org.laxture</groupId>
        <artifactId>sbp-spring-boot-starter</artifactId>
        <version>-SNAPSHOT</version>
    </dependency>

@j-barata
Copy link
Contributor Author

j-barata commented Jul 29, 2020

Thanks, I was able to download the new version.

When I tried the new feature, I got the following messages :

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'resourceHandlerRegistrationCustomizer': Unsatisfied dependency expressed through field 'resourceProperties'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.autoconfigure.web.ResourceProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Field resourceProperties in org.springframework.boot.autoconfigure.web.servlet.PluginResourceHandlerRegistrationCustomizer required a bean of type 'org.springframework.boot.autoconfigure.web.ResourceProperties' that could not be found.

From what I've seen in your recent changes, I can't guess what I could have missed (I basically only added some new configurations in the application.yml).

I tried both development and deployment modes and the result is the same.
I also tried both 2.2.0.RELEASE (SBP's spring boot version) and 2.3.1.RELEASE (my sample project's spring boot version) and the result is the same.

@hank-cp
Copy link
Owner

hank-cp commented Jul 30, 2020

I should have explained this supports a little bit more...

Sbp supports resource handling based on Spring Boots' WebMvcAutoConfiguration. So you will need the almost same configurations as enabling resource handling as Spring Boots do, that will be:

spring:
  mvc:
    static-path-pattern: /public/**
  resources:
    add-mappings: true
    cache:
      period: 3600

full version here: https://github.com/hank-cp/sbp/blob/31614cf79ce3c25ac06f5dfe118469894aa1fffb/demo-app/src/main/resources/application.yml

hank-cp added a commit that referenced this issue Jul 30, 2020
@j-barata
Copy link
Contributor Author

Hello,
I switched to version 0.1.7 and it works fine !

Thanks a lot for your support.

@DobrolubovaJane
Copy link
Contributor

Hi @j-barata ! I have the same problem when Spring Bootstrap try to createController

java.lang.ClassCastException: class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping cannot be cast to class org.laxture.sbp.internal.PluginRequestMappingHandlerMapping

What should I do to fix it?

@j-barata
Copy link
Contributor Author

Hi @DobrolubovaJane

All you have to do is to switch to 0.1.7 version (or upper) and to set this configuration in your "application.yml" :

spring:
mvc:
static-path-pattern: /public/**
resources:
add-mappings: true
cache:
period: 3600

You can also have a look at this sample project : https://github.com/j-barata/marketbasket-sbp-sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants