How to get local to spring boot to localize messages? - spring

How do i get the local of the user so that i can send proper localized message in response pdf file? Autowiring not working properly
#Autowire
private final Local local

You can get the Local in the controller
#Controller
public ResponseEntity controllerMethod(Locale locale) {
}

Related

Unable to upload MultipartFile from Quarkus to Spring boot Application

I am facing a problem where I try to upload file from Quarkus application to Spring boot application. Uploading works but I am unable to pass filename, content-type and size with byte array.
Spring boot application is defined to receive #RequestParam("content") List<MultipartFile> content and my Quarkus Resteasy Reactive Client is defined to send #MultipartForm MyRequest myRequest
MyRequest looks like this currently:
#Data
#NoArgsConstructor
public class MyRequest {
#RestForm("type")
#PartType(MediaType.TEXT_PLAIN)
public String type;
#RestForm("content")
#PartType(MediaType.APPLICATION_OCTET_STREAM)
public byte[] content;
}
by adding #RestForm("filename") public String filename does not solve this problem.
Looking at the code at the Spring Boot end I can see that the MultipartFile is destructed to be something else...
String firstFilename = contents.get(0).getResource().getFilename()
NOTE: What I have to work with is name of the file, input stream, mime-type and size nothing more. I am aware about the reactive client not being able to upload lists.
SIDENOTE: Idea is to receive these four values and then resend the file onwards to the next api which is the Spring Boot application.

Unable to access spring-docs-open-ui swagger documentation

I am a spring newbie trying to configure swagger documentation for my spring boot application. I configured my application based on the documentation provided here
I am able to access the documentation page locally from this URL
http://localhost:8080/doc.html
However, when I deploy my application on Docker I keep getting a 404 status.
https://www.mywebsite.com/context_path/doc.html
My application.properties file looks like this -
springdoc.api-docs.path=/doc
springdoc.swagger-ui.path=/doc.html
I have also added a HomeController which redirects to the documentation page
#Controller
public class HomeController {
private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);
#RequestMapping(value = "/")
public void redirect(HttpServletResponse response) throws IOException {
response.sendRedirect("/doc.html");
}
}
FYI, I am using Amazon Cognito. I have read and tried several examples that I found online but I cannot make this work. Can someone help me?
You are using only #Controller annotation;
If you are using REST APIs, you should use #RestController.
If you need to use #Controller, you should add #ResponseBody
If you don't want change your #Controller, you add:
static {
SpringDocUtils.getConfig().addRestControllers(HelloController.class);
}
More information are available on the documentation of F.A.Q:
https://springdoc.org/faq.html#my-rest-controller-using-controller-annotation-is-ignored

404 error when attempting a REST GET call to a controller in Spring

I'm attempting to make a REST call to a local Tomcat9 server using Spring. My controller is as follows.
#RestController
public class HelloWorldController {
#RequestMapping(method=RequestMethod.GET, path="/hello-world")
public String helloWorld() {
return "Hello World";
}
}
However, when I attempt the REST requesting using the url, http://localhost:8080/hello-world, I receive the following 404 error:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
How can I resolve this? I'm trying to gain more practice with Spring.
EDIT: I resolved the issue. I stopped the Tomcat server and ran RestfulWebServicesApplication as a Java application.

How can I set global context variables in Spring?

So I have a Spring boot application with many api requests.
For a large number of these requests I know want to log the "user-agent" part of the header from the request.
One solution is to do this.
In my controllers I could just put #RequestHeader("user-agent") String userAgent and pass it on to the service layer to be logged.
But it would be much handier if I could add the user agent as a global variable, just like the username is added as a global variable through the SecurityContextHolder.
So my question is, is it possible to add custom global context variables like the authentication details from the Authentication filter class? And if so how?
If you are using Spring MVC then you can Autowire HttpServletRequest and get the request headers from it.
#Service
public class HelloService {
#Autowired
private HttpServletRequest httpServletRequest;
public void print() {
System.out.println(httpServletRequest.getHeader("x-test"));
}
}
Alternatively you can also get hold of request instance from RequestContextHolder:
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getHeader("x-test");

Is it possible to load properties from a web service during spring boot application startup?

I am building a new spring boot application deployable to bluemix (cloud foundry) which needs to do the following:
use spring-cloud-cloudfoundry-connector to discover user-provided "properties service": read the service URL and credentials from VCAP_APPLICATION env variable.
This step is completed.
connect to properties service via HTTP call, receive JSON response, parse individual property values and expose them as application properties (in Environment object?)
What would be the correct solution for this in spring-boot app?
In older non-boot Spring app, the property service call would be initiated early in Spring lifecycle by a class that extended PropertySourcesPlaceholderConfigurer and the property collection from the service would be handled inside postProcessBeanFactory() method call of the same class.
public class CustomPopertiesFactory
extends PropertySourcesPlaceholderConfigurer
implements EnvironmentAware {
private Properties properties;
getServiceCredentials() {
// parse VCAP_APPLICATION json
final String localVcapServices = System.getProperty("VCAP_SERVICES");
// extract url, username, pwd to connect to the service
}
connectToService () {
// via HTTP request using RestTemplate
// parse JSON response and add properties to this.properties
... this.properties.put("prop1", valueFromJson);
}
#Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
getServiceCredentials();
connectToService();
// load values from properties service into app properties
setProperties(properties);
// continue with lifecycle and load properties from other sources
super.postProcessBeanFactory(beanFactory);
}
}
That was painful to maintain and switch between cloud and local spring profiles IMO and I am wondering if spring boot has a better way of handling external properties.
I ended up replacing "properties service" with spring-cloud-config-server
and using spring-cloud-config-client
in my spring boot application to consume properties from spring-cloud-config-server.

Resources