I have a Dropwizard/AngularJS website. I have my assets served from an AssetsBundle on the root directory:
public void initialize(Bootstrap<WebsiteConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/assets/", "/", "index.html"));
...
}
And I want to serve my REST endpoints on /services:
public void run(WebsiteConfiguration configuration, Environment environment) throws Exception {
environment.jersey().setUrlPattern("/service");
...
}
Based on documentation and answers I've seen elsewhere, this seems like it should work. However, I just tried it, and everything on /service is returning a 404. When I dug into the app through a debugger, it appears that it's because the requests are going through the AssetServlet, not through Jersey.
Am I missing something? Is there a good way to serve my front-end on the root directory and the REST services on a sub-path?
I also struggled with this issue some time ago. What helped me was adding these lines to the app.yml config file:
server:
type: simple
rootPath: '/rest/*'
applicationContextPath: /
Related
I'm doing a GUI in Angular which is calling a REST-api written in SparkJava (JavaSE). Everything will be running on the same machine.
The GUI was originally written in swing but I'm rewriting it in Angular to get a more modern touch.
Problem:
When the application starts everything seems to be loaded as expected. When I open the web page in a browser I get an INFO-message in the Java-console stating spark.http.matching.MatcherFilter - The requested route [/sv/assets/images/image1.png] has not been mapped in Spark for Accept: [image/webp,*/*], among other similar messages.
The Angular files are located in src/main/resources/public. There are a few subfolders with content, see screen shot.
The backend is served as it should, but the frontend files can't be loaded properly in the browser. HTML/JS/CSS seems to be loaded correct, but images and other files in assets folder can't be loaded.
Can this be solved? Or do I have to put all files in the root directory? I found nowhere all static files in SparkJava have to be located in the same folder (meaning, no subfolders).
My setup:
Spark.port(4567);
Spark.staticFiles.location("/public");
// simple rest API to serve some data
Spark.get("/labels/getall", "application/json", new Route() {
#Override
public Object handle(Request rqst, spark.Response rspns) throws Exception {
Labels l = LabelReader.getLabels();
Gson gson = new Gson();
return gson.toJson(l);
}
});
File tree:
Java console messages
Browser output
I'm trying to make Spring Security permit access to static resources to all users, but for now nothing works.
When I used jsp in previous project, the solution was simple:
http
.authorizeRequests()
.antMatchers("/static/**").permitAll()
.anyRequest().authenticated()
Static folder was placed inside webapp folder which was the root folder and was easily detected by Spring Security. Now, because of Thymeleaf, there is no webapp folder and all the static folders are placed into src/main/resources. I have no idea, how to create antMatcher for something that is inside resources folder... I tried that:
http
.authorizeRequests()
.antMatchers("resources:/static/**").permitAll()
.anyRequest().authenticated()
It never worked. What is the solution?
ps. I have seen a statement that Spring Boot + Spring Security allows this intra-resources access by default, but it does not.
The solution is found. For my folder structure src/main/resource/static/css I should have used
.antMatchers("/css/**").permitAll()
instead of
.antMatchers("/static/**").permitAll()
Check my answer there: Spring boot mapping static html
Basically you have to add resource handlers by extending WebMvcConfigurerAdapter to map http://yoursite/static_url_prefix to your static_app_dir directory in your resources directory.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static_url_prefix/**").addResourceLocations("classpath:/static_app_dir/");
super.addResourceHandlers(registry);
}
This will intercept all request coming to http://yoursite/static_url_prefix and return results from classpath://static_app_dir in your jar file or from /resources/static_app_dir when running application from your IDE.
Spring security can be configured as before as it has nothing to do with it i.e. your first code example seems correct.
I am trying to build a very basic Restful server in Eclipse using jersey and GrizzlyHttpServerFactory. Below is the code:
The server
package com.example.rest;
class WebServr{
public static final String BASE_URI = "http://localhost:8080/myapp/";
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in com.example.rest package
final ResourceConfig rc = new ResourceConfig().packages("com.example.rest");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
public static void main( String[] args ) {
WebServr wb = new WebServr();
wb.startServer();
}
}
The rest component
package com.example.rest;
#Path("/amp")
#Produces("text/html")
public class Endpoint{
#GET
#Path("/test")
public Response test() {
return Response.ok().entity("SUCCESS").build();
}
}
But when I create a simple “jar” file by exporting my compiled java files using above code, I can see my server is running(can see output in netstat –an | grep port) but my restful client is returned with “404 error”(though I can see a new http connection is being established at server side).
But when I create a jar file using eclipse option of “Runnable jar”, it works perfectly fine(my client is served properly).
Can anyone give any suggestion why it is happening.
i am facing the same issue,
The short answer :
place your compiled classes in web-inf/classes
The long version :)
I am convinced that because of ClassLoader in tomcat, web-inf/classes is being loaded before web-inf/lib,
when you place your classes in web-inf/classes it's being loaded by classloader, then when web-inf/lib jars are loaded which jersey one of them, jersey will scan for the declared classes, and will find them (WIN)
BUT
when classes are in a jar file (in web-inf/lib) they will be loaded yes, but MOSTLY they are loaded after jersey, and as a result when jersey scans for the endpoints it will find nothing.
i know this question is a bit old, but this could help someone facing same issue.
For a solution, actually i am still looking, if i can control the order of jars loading, or i may implement some classes (Application, Configuration) from jersey and figure out a way to let jersey find them.
I work on spring boot application. I'm trying to serve static content with spring.
want to serve a resource stored in the /c:/frontend/files/ directory whenever a request comes in for the URL matching the pattern: /file/**:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/file/**")
.addResourceLocations("file:///C:/frontend/files/" );
}
but when i try to access to this resource using this url: http://localhost:9999/file/app.min.js
I have this problem
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation
I resolved the problem. it's related to "spring-cloud-config-server". I just delete this config: org.springframework.cloud spring-cloud-config-server
It sounds like your project's folder structure is wrong.
Code should go under src/main/java and resources (like your javascript) should go under src/main/resources. You have a few different options where you can actually serve the files from. This post on the spring.io blog has the following to say:
Spring Boot will automatically add static web resources located within any of the following directories:
/META-INF/resources/
/resources/
/static/
/public/
Another option you also have is using webjars.
Personally, I've found it easiest to put those kind of files under src/main/resources/public. It always works without any issues for me. The interesting thing is you can put a folder named /public anywhere in your project and spring-boot will serve files out of it. You have to be really careful that it's under src/main/resources/public though if you're using a build tool like maven, as when you come to build your .jar the files won't be in the right place otherwise.
I have created two webservices clients in NetBeans 7.1, mimicking the tutorial they have on their website.
I had done the first one a while ago against a wsdl file located at a http location, and had no problem using the webservice
The webservice I am working with at the moment has a wsdl file located at a https location. The setup of the service went off without a hitch. The only difference with the first one was a popup alerting me to a security certificate, which I accepted. Service, Generated Sources, META-INF etc are all created successfully. A local copy of the wsdl file is stored on my pc under the src/.. folder.
However, as soon as I go to run my code, I receive this error:
Cannot find 'https://-domain-.net/-XYZServices-/-ABCXML?wsdl-'
wsdl. Place the resource correctly in the classpath.
I found several similar issues in Stackoverflow and other places online, but nothing that really addressed my problem. I tried several suggestions anyway:
I checked the jax-ws-catalog.xml file, and found the url quoted above mapped to the local folder where the local copy of the wsdl is stored.
I checked whether that local folder actually contained the wsdl file - it did.
I tried editing the url in the jax-ws-catalog.xml file to point to port 8080 and 8081 - no joy.
I guess it is a security issue, but have no clue as to how to fix this.
Can anyone point me in the right direction here.
FYI: I'm fairly new to java and NetBeans.
Thanks.
The best way to avoid the error "Cannot find wsdl. Place the resource correctly in the classpath." is to use wsdllocation to specify the location of the wsdl and also to package the wsdl as part of the jar.
When you specify the wsdllocation make sure you add "/" to the beginning of the location.
wsimport -keep -Xnocompile -wsdllocation /schema/10.0/MyService.wsdl schema/10.0/MyService.wsdl
Just put your WSDL file in your classpath, etc., src/main/resources/MyWsdl.xml and use this to get it:
URL url = new URL(baseUrl, "classpath:MyWsdl.xml");
Also do not forget to add this on your service class that extends javax.xml.ws.Service:
#WebServiceClient(name = "MyService", targetNamespace = "http://example.org/", wsdlLocation = "classpath:MyWsdl.xml")
Not sure if this helps, but...
From Here
On the client side to consume SSL enabled Web service:
- in the New Web Service Client wizard under WSDL and Client location specify the WSDL file of the Web Service by setting WSDL URL in form
of https://:8181//
- then right click on the created web service and choose Edit Web Service Attributes and under Wsimport Options correct the wsdlLocation
option to the following form:
/META-INF/wsdl/_8181//.wsdl
Whenever you refresh the web service a fresh wsdl file gets loaded
from the deployed application and the wsdl file gets loaded as a
resource defined by the correct path (mentioned wsdlLocation option
value).
Make sure that you have configured your web service.
One way to do so is to implement a class that extends javax.ws.rs.core.Application. That is, add a class which is similar to the following:
import java.util.Set;
import javax.ws.rs.core.Application;
#javax.ws.rs.ApplicationPath("rest")
public class ApplicationConfig extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(rest.HelloWorld.class);
resources.add(rest.Search.class);
// Here continue adding all the JAX-RS classes that you have
}
}