Add prefix in #RequestMapping annotation in Spring Framework - java

I'm working in Spring Framework 5 in Eclipse and I need to add a prefix to many of my #RestControllers, so I found that placing this: #RequestMapping("${foo.bar}") at the beginning of the class and adding a new file application.properties to main/java/resources will solve my problem.
The content of application.properties is:
foo.bar=/test
But it doesn't work because when I try to access on my web explorer to: localhost:8080/project/test I get a 404 Error. It works only if I access with localhost:8080/project/$%7Bfoo.bar%7D. I really don't know what I'm doing wrong. I don't know if Spring will detect automatically the properties file or not.
Can someone help me?

The values from the properties file are accessed from the spring bean. But the #Restcontroller is used to register the bean that provides the restful service.
And for request mapping the controller, you just have to hardcode the value like this as at this point you can't get the message resource object yet.
#RestController
#RequestMapping(value = "/test")
Just for the information, to access labels from the properties file inside the controller you have to #Autowired a MessageResource object in the RestController
#Autowired
private MessageResource;
and access the labels of the properties file like this in any of the controller methods.
message = messageResource.getMessage("foo.bar");

Related

requestmapping invalid after configuring entity path

I have a project called models which i import via maven, it consists of Hibernate Entities,
I setup this models project as a dependency on a project that i have configured Spring, it uses spring boot.
if i try to run the project it tells me that Not a managed type:when i reference an entity from the models project. i managed to fix it using the following annotations
#EnableJpaRepositories({"com.rajeeda.coopmis.models.*", "com.rajeeda.coopmis.web.*"})
#ComponentScan(basePackages = { "com.rajeeda.coopmis.web.*" , "com.rajeeda.coopmis.models.*"})
#EntityScan({"com.rajeeda.coopmis.models.*", "com.rajeeda.coopmis.web.*"})
```
once i setup the path to models then spring boots up fine, but none of the #RequestMapings from spring is being reached. as i get a 404 screen, i tried seven specifying path using
server.servlet.contextPath=/mainstay`
still no luck, is there a way i can explicitly provide the path of controllers?
if i remove the above annotations from the Application class then the end points works but i get the not managed entity error (i test and confirm the controllers are working when i remove the following class
public interface UserRepository extends CrudRepository<ItUserMaster, Integer> {}
if anyone has faced the same problem please do share. thanks in advance
Not sure this will solve the issue, but you can give it a try.
Let us use basePackages and remove the wildcard like this:
#EnableJpaRepositories(basePackages = {"com.rajeeda.coopmis.models", "com.rajeeda.coopmis.web"})
#ComponentScan(basePackages = { "com.rajeeda.coopmis.web" , "com.rajeeda.coopmis.models"})
#EntityScan(basePackages = {"com.rajeeda.coopmis.models", "com.rajeeda.coopmis.web"})

Spring Boot read value from application.properties

In my application.properties I have defined port and server context.
server.port=8080
server.context-path=/SekcjaN
How to read value context to insert it into RequestMapping value in controller.
#RestController
#RequestMapping(value = server.context-path+"/auth")
public class AuthController extends BaseController {
}
You don't need to add server.context-path into your controller RequestMapping as it is already handled by spring boot as base path. So if you are using server.context-path as something all your mapping will be prefixed with server.context-path value.
You can just use your code like this
server.port=8080
server.context-path=/SekcjaN
Controller file looks like
#RestController
#RequestMapping(value = "/auth")
public class AuthController extends BaseController {
}
Your code will perfectly be called by using <domain>:<port>/SekcjaN/auth/..
You simply don't, it's already implied. The mappings are relative to the web application context path (to be precise: relative to the dispatcher servlet that is by default mapped on the root path within the application). server.context-path configures the path where the entire application is available within the server.

SpringCloudConfig When I use these annotations #RefreshScope #ConfigurationProperties ,I was in trouble

SpringCloudConfig trouble
When I use these annotations #RefreshScope #ConfigurationProperties, I was in trouble.
#Component
#RefreshScope
#ConfigurationProperties(prefix = "config.message")
public class MessageProperties {
private int max_num;
private boolean begin;
private String ding_department;
// getter, setter...
}
like this ! Config does not work;
but when I use only #ConfigurationProperties,it works. So What's the use of #RefreshScope. And how to fix it?
So when you use "I was in trouble" in stackoveflow you are in trouble that there is high probability no one to answer.
#ConfigurationProperties is used for mapping properties to a POJO, with prefix you start using hierarchical properties structure. SO for example based on your description your code will work if you have the following .yml
config:
message:
max_num:
begin:
ding_department:
If for example you use spring-cloud-config server to store the configuration properties and spring-boot and want on change on the file the corresponding Bean with injected conf file to be update you add #RefreshScope, but even if you do this the bean is not updated you have to call the /refresh url or to trigger event which will refresh it.

Spring: define #RequestMapping value in a properties file

Is it possible to define the value of a #RequestMapping annotation in Spring by defining it in a properties file?
Actually, I do something like:
#Controller
#RequestMapping("/xxx")
public class MyController {
...
}
But I would like to store the path /xxx in a properties file. Why? For instance, it is less likely that I do mystakes in my templates if I rename the path in the controller.
In other framework this is allowed (see Symfony, for instance).
It should be possible to use placeholders in #RequestMapping, like for example #RequestMapping("${foo.bar}"). Take a look at the documentation for more details:
Patterns in #RequestMapping annotations support ${…​} placeholders against local properties and/or system properties and environment variables. This may be useful in cases where the path a controller is mapped to may need to be customized through configuration. For more information on placeholders, see the javadocs of the PropertyPlaceholderConfigurer class.
Thx for the help. It is my contribution...
No dependencies are necessary because maven do everything by itself.
In the property file - use maven interpolation, such as below:
vs= v1
us= users
me= messages
url.user=${vs}/${us}
url.mess=${vs}/${me}
In your destiny file, for example controller/resource (in mycase):
#RestController
//#RequestMapping("v1/users") <<<<<<instead this
#RequestMapping("${url.user}")<<<<<<use this
#Api(value = "API RESTFUL)
public class UserResource {
//
As bohuslav burghardt has mentioned this is totally possible.
So if you have a common domain stored in your application.properties file you can use placeholders to call it in your controller/s and even chain placeholders & text together.
For Example...
In your .properties file
app.domain = mydomain/v1
In the controller
#RestController
#RequestMapping("${app.domain}/my-controller")
public class MyController {

Disable replacement of XML eXternal entities in Spring Jaxb2RootElementHttpMessageConverter

I have an annotation based Spring web application controller which maps XML to an annotated plain old java object. Exemplary code as follows:
#Controller
#RequestMapping("/")
public class Endpoint {
#RequestMapping
public void send(...) {
...
}
...
The mapping is automatically done via Jaxb2RootElementHttpMessageConverter and at the moment it is possible to reference and replace content in the received XML.
My question is, is it possible -- for example via beans.xml -- to configure the Jaxb2RootElementHttpMessageConverter such that the replacement of entities is prevented?
The converter is automatically created by Spring.
If you want to overwrite a bean created by spring you just define your own bean of the same type. Additionally you can mark it #Primary. I haven't tried it with Jaxb2RootElementHttpMessageConverter, but it worked with ObjectMapper as described here :
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper

Categories