I am working on a existing project where the restControllers has a restmapping #RequestMapping(value = "/test"). There is no base URL added. I have checked all the option where the baseURL gets added but i dont find any in my application. When i run my server. The way to access is <hostname>/rest/v1/test.Application also uses Spring Hateoas. Can you let me know from where/how do these additional /rest/v1 is getting added?
You should find it configured in web.xml or some spring config xml where the servlet mapping url is done.
If the project is generated using maven then you should see the application name in pom.xml file something like this.
<groupId>com.test</groupId>
<artifactId>rest</artifactId>
<packaging>war</packaging>
<version>0.1.0.BUILD-SNAPSHOT</version>
<name>rest</name>
and also check web.xml file inside webappp/WEB-INF directory or any other XML config file if you have.
Probably you have one of these property set in the application.properties file:
server.contextPath=/rest/v1
or
spring.data.rest.basePath=/rest/v1
You can control the base path of you application from there.
Hope it helps!
someone configure it somewhere. search for it in:
application.properties
server.servlet-path = or spring.data.rest.basePath =
Configuration file: just like #Arnad said
Configuration Classes: search for a bean named RepositoryRestConfigurer
search in whole project:
in eclipse use Ctrl + h -> File search
in intellij: ctrl + shift + f
ref link : http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_changing_the_base_uri
With very minimal code that you have added, I can only tell you possible place where the base url might have been configured.
On top of your #RestController annotated class.
In case of WebApplicationInitializer it must have been added on ServletRegistration.Dynamic something like below:
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/rest/v1/");
Related
I am running hibernate search with spring boot. I have written a working configuration for my application. How ever, i want to externalize my configuration and use ./config/hibernate.properties instead of src/main/resources/hibernate.properties. After copying my properties file to the desired location, i am getting and exception:
nested exception is java.io.FileNotFoundException: class path resource [hibernate.properties] cannot be opened because it does not exist
Anyone with any idea on how i should tell spring to read my configuration file?
Move your configuration to an src/main/resources/application.properties file and prepend spring.jpa.properties. everywhere, so hibernate.dialect will become spring.jpa.properties.hibernate.dialect, for example.
Then you can use Spring features to move your configuration wherever you want. To move it to ./config/application.properties I suppose you will have to add #PropertySource("./config/application.properties") to one of your #Configuration classes, or something similar.
I'm sure you can also keep the hibernate configuration in a separate file (separate from the rest of your application configuration).
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details about externalizing configuration in Spring Boot.
For some reason, it seems hibernate-search will prevent application from starting as long as a hibernate.properties configuration file does not exist. After trying for a while without success, i found a work around for my problem.
First, i created an empty hibernate.properties file and place it under src/main/resources.
Secondly, i moved all hibernate-search configurations to application.properties as follows:
spring.jpa.properties.hibernate.search.default.indexmanager = elasticsearch
spring.jpa.properties.hibernate.search.default.elasticsearch.host = http://my-server.com
spring.jpa.properties.hibernate.search.default.elasticsearch.index_schema_management_strategy = CREATE
spring.jpa.properties.hibernate.search.default.elasticsearch.required_index_status = yellow
This way, the application will start and spring will get all configuration from the externalized configuration as documented here.
i have created springboot project which gives fat-jar. i want to push external xml file in runtime into it.i want to place that xml file into spring-boot-tomcat container. tried many ways to do it (#import, --spring.config.location,etc) those ways didn't work out for me.
That xml file is ApplicationInsight.xml, which is used to post telemetry from our application to Azure portal.
Highly appreciate any help.
Based on the GitHJub issue, I think part of the problem is how you are passing JVM parameters, and how you are using "spring.config.location".
I am not familiar with Azure Insights really, but if I understand correctly, it is trying to load the ApplicationInsights.xml file to configure itself, and it's doing this automatically. So you really can't set it up in the WebConfigurerAdapter as I previously suggested because it has already initialized itself before that, correct? I left that part in anyways, but I get that it needs to be loaded sooner so I provided a few additional ways to add the file to the classpath ASAP.
New Stuff
First take a look at this line you had originally posted ala GitHub:
java -jar build/libs/file-gateway.jar --spring.config.location=classpath:/apps/conf/ApplicationInsight.xml
Instead the value should be just a folder path, without "classpath" of "file" prefix. Also, try using '-D' instead of '--'.
java -jar build/libs/file-gateway.jar -Dspring.config.location=/apps/conf/
The property is supposed to either refer to a directory containing auto configuration property files for Spring Boot. It can also work for referring to a specific "application.properties|yml" file.
With that, my previous suggestion may work for you.
Old Suggestion
If you require a unique way for loading resources, you can add a resource handler to your application.
#Configuration
#EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
#Value("${telemetry.folder}")
private String telemetryFolder;
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceLocations(telemetryFolder);
}
}
And/or you could load it with apache IO:
#Value("${telemetry.file}")
private String telemetryFile;
#Autowired
private ResourceLoader resourceLoader;
public String telemtryXml(){
return org.apache.commons.io.IOUtils.toString(resourceLoader.getResource(telemtryFile).getInputStream());
}
But this will only work if the api you are using doesn't need to be initialized much earlier.
More New Stuff
In your last post on the GitHub issue, you tried this:
java -jar build/libs/file-gateway.jar -applicationinsights.configurationDirectory="/apps/conf/"
Instead, try adding the property as a jvm parameter like this:
java -jar build/libs/file-gateway.jar -Dapplicationinsights.configurationDirectory=/apps/conf/
Notice that I added a capital 'D' character after the, and I removed the quotes from the path.
Other ways to add the file to classpath are.
Add the directory to the JVM classpath.
java -cp "build/libs/file-gateway.jar:/apps/conf/*" your.package.MainSpringBootApplication
This requires that you specify the main class which is (commonly) annotated with '#SpringBootApplication' and contains the main method. You do not execute the jar like before, but you do still add it to the classpath.
Forget about SpringBoot, and go back to your roots as a JEE developer. Add a "context.xml" for your app under the "src/main/resources/META-INF" folder, or "src/main/webapp/META-INF". I prefer the later if I'm building an executable war file, and the former for jars.
Example context.xml:
<?xml version='1.0' encoding='utf-8'?>
<!-- path should be the context-path of you application.
<Context path="/">
<Resources className="org.apache.catalina.webresources.StandardRoot">
<PreResources base="/apps/conf"
className="org.apache.catalina.webresources.DirResourceSet"
internalPath="/"
webAppMount="/WEB-INF/classes"/>
</Resources>
</Context>
You can also use JVM parameters with EL.
So if you execute the jar with this:
java -jar build/libs/file-gateway.jar -Dapplicationinsights.configurationDirectory=/apps/conf/
You could set the resources base with this:
<!--snip -->
<PreResources base="${applicationinsights.configurationDirectory}"
<!--snip -->
Hope that helps:)
I have two projects - one is based on the "get started" example, second is from the spring-boot-samples. I build both with Maven and run both from Eclipse. The "spring-boot-samples" project loads application.properties and displays Velocity templates named by the Controller. The "get started" does not.
Same file structure for application.properties (src/main/resources/application.properties) and templates (src/main/resources/templates/**), both with src/main/resources set to "Use as Source Folder" in Eclipse. Same workspace, same JRE.
I compared the .classpath and pom.xml, but found nothing suspicious. Obviously there's a difference, but where do I have to look?
PS: I can load application.properties via #PropertySources, but
that should not be neccessary (see comments there)
is not necessary in the "spring-boot-samples" project
does not help concerning the Velocity templates
Thanks!
To fetch values from application.properties in spring boot, we need to specify some annotation.
application.properties must be in the path src/main/resources
class must contain #RestController annotation
#Value("${name}")
private String name;
Ah, one important difference:
#RestController delivers the response directly, meaning: Instead of resolving the template's name the String is passed to the browser. The reason should be #ResponseBody:
Annotation that indicates a method return value should be bound to the web response body.
Using #Controller instead solves the Velocity problem.
EDIT:
To close this thread: I will continue using #PropertySources to get application.properties, but it does not work without it. Just having application.properties in your classpath is not sufficient.
I have a Spring MVC application and in it I am running a periodic job using a class with method annotated as #Scheduled
In this method, I want to get the base application path i.e. http://localhost:8080/ or http://www.mywebsite.com/ based on whether this is my local system or production system.
How can I do this? I do not have access to HttpServletRequest because this is not a Controller class.
Any hints would be appreciated
In my opinion it is a good idea to use profiles and store properties like base application path in properties file - where each environment has its own property file: config_dev.properties, config_production.properties
Once they are there you can load them in job-like classes using Environment (described on SpringSource blog).
How to configure Tomcat and Spring to use profiles: Spring 3.1 profiles and Tomcat configuration
Put a myconfiguration.properties out of your application, to let the application know that whether its running locally or in production. And then in your method annotated as #Scheduled just read the Property file.
String configPath = System.getProperty("config.file.path");
File file = new File(configPath);
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
And provide the agrument,
-Dconfig.file.path=/path/to/myconfiguration.properties
when running your application server (or container). This can be done by putting,
JAVA_OPTS="$JAVA_OPTS -Dconfig.file.path=/path/to/myconfiguration.properties"
at the beginning (roughly) of the script, which is used while running your application server.
For tomcat its catalina.sh
For Jboss AS its run.sh
For weblogic its setDomainEnv.sh
And After doing that start your server and deploy your application. Finally, your #Scheduled method should know the information it needs. As the property file is outside of the application, you can change the value of the property when you want without rebuilding the application or without even disturbing it!
just add this code in your web.xml
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>my.root.path</param-value>
</context-param>
and use it your code as a system properties
For my simple maven project this doesn't work:
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
config.xml is resided at the same class level
How,actually,add config.xml to classpath?
note: my project is a lib,if I do the same in other web project with configuration in
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config.xml</param-value>
</context-param>
that it works OK
Here I needn't web.xml, just correct classpath.
When you enter classpath*:config.xml, the classpath* is a wild card indicates that you want to load every file matching config.xml on the entire classpath, not just the single file config.xml. This may be why your solution is working otherwise.
When instantiating a new ClassPathXmlApplicationContext, try giving the full classpath as an argument: com\sergionni\myproj\config.xml.
If your config xml is in package com.anywhere.here then try this:
ApplicationContext myAppContext = new ClassPathXmlApplicationContext("com/anywhere/here/config.xml");
In case of maven project, right click on project-maven-update project, this helped me solve my issue.
Please do This code - it worked
AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
o/w: Delete main method class and recreate it while recreating please uncheck Inherited abstract method its worked