Following code below is Spring Boot Serving Images Configuration.
#Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
/*registry.addResourceHandler("/images/**").addResourceLocations("file:/home/test/images");*/
registry.addResourceHandler("/images/**").addResourceLocations("file:///C:/test/images/");
}
#Override
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Till Yesterday, It was serving properly.But all of sudden its serving like this.
If I try to access same image using file path url its working fine eg
file:///C:/test/images/profile/5880b70d91286e0318863818/APM1243//originalImage_k3kf6nxil1en8yi_.jpeg
Please help me?
Its not because of backend code(Java), image was not displaying properly. Its because of browser, when I do empty cache reload image is rendering properly.
Related
I am using Angular, Spring Boot, and WebSocket in my project. I'm testing on my local machine socket connection is working fine but when I deployed my build on the production server it is not working properly. sometimes connection was established and sometimes not. I'm new to this technology.
I already configured the message broker and WebSocket application endpoints here is my code
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer, WebSocketConfigurer {
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").setAllowedOrigins("http://localhost:4200", "https://www.xxxxxx.com/").withSockJS();
}
#Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/chatparticipant", "/messages", "/unAssignedParticipant");
}
#Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxBinaryMessageBufferSize(1024000);
return container;
}
#Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new WebSocketHandler(), "/socket").setAllowedOrigins(Pl4ChatConfig.ALLOWED_ORIGIN);
}
}
we're getting error messages:
WebSocket connection to 'wss://xxxxx.com/ws/709/2pjkwf4q/websocket' failed:
I have a vuejs + spring boot app. All was working fine, but suddenly got this issue - requests to files in /js/, /css/, /img/ are returning the index.html content despite having a resource mapping pointing to classpath:/static.
Can't trance the original change which lead to the appearance of this problem. front-end works fine by itself (tried deploying to surge & zeit now), so i suppose the problem is that spring boot ignores the resource mapping.
spring boot v2.1.2
WebMvcConfig:
#Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
String baseApiPath = "/api";
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**/*.css", "/**/*.html", "/**/*.js", "/**/*.png", "/**/*.ttf")
.setCachePeriod(0)
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/")
.setCachePeriod(0)
.addResourceLocations("classpath:/static/index.html")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
#Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
if (resourcePath.startsWith(baseApiPath) || resourcePath.startsWith(baseApiPath.substring(1))) {
return null;
}
return location.exists() && location.isReadable() ? location : null;
}
});
}
}
in index.html links like this <script src=/js/chunk-vendors.b7114b0e.js></script><script src=/js/app.5c7ddca5.js></script> returning the index.html itself.
I am trying to load js files in my spring boot application, but when I try to load "/resources/static/js/jsfile.js" using the url http://localhost:8080/js/jsfile.js I am getting a 404 not found error.
I need to prevent this controller from getting called when I try to load something in src/main/resources/static/
#GetMapping("/{username}/{slug}")
public String single(#PathVariable("username") String username, #PathVariable("slug") String slug, Model model) {
model.addAttribute("user", postService.getSinglePost(slug, username));
return "/post/single";
}
After reading other solutions I tried creating this but I'm still getting 404 errors.
#Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
registry
.addResourceHandler("/css/**")
.addResourceLocations("/css/");
registry
.addResourceHandler("/img/**")
.addResourceLocations("/img/");
registry
.addResourceHandler("/js/**")
.addResourceLocations("/js/");
}
}
I use Spring 4.2.3 via spring-boot (1.3.0) at server and binaryjs 0.2.1 at client.
Binaryjs successfully send data over websocket to server, but i can't receive it (bytes).
How to declare method, that will be receive byte[] from websocket (and save, e.g. into temporary file)?
Spring websocket config:
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/test");
}
}
I do not use withSockJS because SockJS can't send bytearray data
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/test").withSockJS();
}
Apologies if this feels repetitive, I gather alot of people ask about this but I haven't been able to find an answer that works.
I have a web-app, built using maven. I use Spring 4 MVC to deliver a RESTful Json API. I also have a lot of static content (html, css, js) were I use Angular.js to put a pretty face on the data API.
For the life of me, I can't figure out how to get both of these being served at the same time without messing with their paths.
I'd really like to go to {APP_ROOT}/people/{id} in my browser, and be interacting directly with my REST api without any crap about /api/ or /rest/
I'd really like to go to {APP_ROOT}/css/style.css in my browser, and be served content from src/main/webapp/css/style.css without any crap about resources or static
Additionally, I'd really like to configure all of this with annotated Java classes, and not have any web.xml, application-context.xml, etc.
So, the Spring dispatcher servlet should be handling all of the REST resource paths, and then falling back to the default Tomcat/Jetty handler for the static content. I thought this was exactly the scenario default-servlet-handler was intended for? I can't seem to get it to work.
These are my relevant configuration classes:
WebAppInitializer.java
public class WebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[0] ;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class};
}
#Override
protected String[] getServletMappings() {
return new String[] { "/*" };
}
#Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
return new Filter[] { characterEncodingFilter};
}
}
WebConfig.java
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = {"my.example.package"})
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
With this configuration I can interact with the REST api, but not the static content. The default servlet handler seems to have no effect.
To access static resource like css js or html keep all these files inside webapp folder of your module. Suppose all you static resources are in src/main/webapp/static path you can do mvc resource mapping like below in context xml
<mvc:resources mapping="/index-dev.html" location="/static/index-dev.html"/>
<mvc:resources mapping="/index.html" location="/static/index.html"/>
<mvc:resources mapping="/app.js" location="/static/app.js"/>
<mvc:resources mapping="/appinit.js" location="/static/appinit.js"/>
<mvc:resources mapping="/extjs/**" location="/static/extjs/"/>
<mvc:resources mapping="/app/**" location="/static/app/"/>
<mvc:resources mapping="/static/**" location="/static/static/"/>
<mvc:resources mapping="/static/**" location="/static/"/>
Now since you want to do it without xml you can do it in your WebConfig class like below example
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/index.html").addResourceLocations("/static/index.html");
}