New to spring and have a question regarding setting the Rest URI path.
I have a working rest service and I would like to change the URI from something like http://localhost:8080/myapp/api to http://localhost:8080/api
Normally I would do this with an annotation such as: #ApplicationPath("api") but that doesnt seem to be applicable, tried the below but doesnt give the desired result.
TestController.java
#Controller
#RequestMapping("api")
public class TestController {
#RequestMapping("/test")
public #ResponseBody Test Test() {
return new Test("Test String");
}
}
How can this be done with spring?
Generally configured in META-INF/context.XML. Set path to / or "".
Here's a similar issue with tomcat 7
HOWTO set the context path of a web application in Tomcat 7.0
Related
I am working on Quarkus application and what I want to do is to set the global path from application.properties file for all rest rest, my application is working but while calling rest request it is giving not found 404.
#ApplicationScoped
public class ABC {
#POST
#javax.ws.rs.Path("/callit")
public Uni<Response> deleteNoti()
{
//whatever logic
}
}
#ApplicationScoped
public class PAR {
#POST
#javax.ws.rs.Path("/callitPar")
public Uni<Response> addNoti()
{
//whatever logic
}
}
And in application.properties file I am configuring below properties:
quarkus.resteasy.path=/rest/*
quarkus.rest.path=/rest/*
quarkus.http.root-path=/myapp
but when I am calling rest request from front-end it is not working, my rest request should be as below:
http://localhost:8080/myapp/rest/callit
http://localhost:8080/myapp/rest/callitPar
What I want is every rest request should start with "/rest/*" and my application base URL should be "/myapp", Let me know how can we achieve it?
Try to annotate your resource classes with #Path("/") and set quarkus.resteasy.path=/rest.
This should result in your described behaviour.
quarkus.rest.path can be removed.
I have a health check controller like the below one:
#RestController
#RequestMapping("/api/${app-name}/management")
public class HealthCheckController {
#GetMapping(path = "health", produces = MediaType.TEXT_PLAIN_VALUE)
public Mono<String> healthCheck() {
return Mono.just("Ok");
}
}
I have this controller in a common library and this library is included in all our services. In each service properties, I have given the value for app-name. So the URLs like http://host:port/api/service1/management/health will return Ok. This is working fine on my local machine but on the server, getting a 404 error. Our services are deployed in Kubernetes.
Am I missing anything here? Was the value binding in #RequestMapping (${app-name}) is correct usage?
I'm trying to enable the cross origin header to be able to reach the service from anywhere (only on local env) but I cannot.
#Configuration
#RequiredArgsConstructor
public class CrossOriginConfig implements WebMvcConfigurer {
private final SecurityConfiguration securityConfiguration;
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("*").allowedOrigins(securityConfiguration.getCrossOrginFilter());
}
}
I made a custom index.html with an ajax call and it fails due to the Allow-Cross-Origin header missing and it comes from another origin.
Simple Spring Boot 2.0 controllers are used with #RestController annotation and simple #GetMapping.
What I missed? What should I include and where?
You need to add the below annotation on either on the controller class or the specific method:
#CrossOrigin
By default, its allows all origins, all headers, the HTTP methods specified in the #RequestMapping annotation and a maxAge of 30 minutes is used.
If you want to allow only http://localhost:8080 to send cross-origin requests
#CrossOrigin(origins = "http://localhost:8080")
Replace the host and port accordingly.
Check the below Spring documentation for more information:
https://spring.io/guides/gs/rest-service-cors/
As Georg Wittberger pointed out the problem was with the mapping. I used the * wildcard what is not good for paths.
Instead of this: registry.addMapping("*")
I used this: registry.addMapping("/**") and it's working fine.
Problem:
I have a WebFilter which forwards valid urls to either a proxy servlet or a servlet which handles a webpage for the admin to monitor recent requests and more.
The admin servlet is suppost to forward ajax requests to a REST service (after login.jsp from the webpage rendered by controlpannel.jsp ) but apparently the rest service has a different context as the WebFilter and WebServlets ?
Question:
So is it at all possible to forward from my WebServlet to the rest helper servlet (and its resource classes) ?
More specific Information:
This is how I use forwarding:
ServletContext sc = request.getServletContext();
RequestDispatcher dispatcher = sc.getRequestDispatcher(forwardURI);
dispatcher.forward(request, response);
I tried to forward to this uri:
forwardURI = /REST/proxy_client/newer_than
My rest helper servlet:
#Stateless
#ApplicationPath("/REST")
public class RestService extends Application {
#Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> restResourceClasses = new HashSet<Class<?>>();
restResourceClasses.add(ProxyClientResource.class);
return restResourceClasses;
}
}
And this resource class:
#Path("/proxy_client")
#Stateless
public class ProxyClientResource {
#EJB
private ProxyClientBean proxyClientBean;
#GET
#Produces("application/json")
#Path("/newer_than")
public String getNumberOfEntriesNewerThanTimestamp(#QueryParam("timestamp") Expression<Timestamp> indexTimestamp,
#QueryParam("numberOfclients") Integer numberOfclients) {
List<ProxyClient> pageData = proxyClientBean.getElementsNewerThan(numberOfclients, indexTimestamp);
return convertToJSONstring(pageData);
}
Solution attempt:
I found this question about how to call a rest web service from a servlet, but they use a client and no forwarding.
EDIT:
I had a configuration problem (might still have one), so now when I try to forward to my rest helper servlet (the one extending javax.ws.rs.core.Application) I get this error:
RestServlet is currently unavailable
(in the web.xml I call the Servlet RestServlet)
when accessing the REST api directly I get:
HTTP Status 500 - Authenticator.invoke() failed
but I can't find out what this means.
Edit2:
I will try repacing the subclass of Applicaton with a config in web.xml subclassing and #ApplicationPath dont seem to work for me. Also when I try to get the rest ServletsContext I get an error that no class has been specified, which is something you do when using the web.xml config.
Edit3:
I'm deploying my application on HCP and with the underlying problem beeing that I cant even access my REST service I found this SAP discussion.
When I get my REST service working without forwarding I will report back here.
Edit4:
This actually answers the question from Edit3
I had to add jersey 1.19.1 (not 2.x because im using Java EE6 which only supports up to DWP 3.0 not 3.1 as required) to by projects libraries otherwise It would say that I didn't specify a servlet class (but when I tried to add javax.ws.rs.core.Application it would tell me this is no Servlet class even though I have seen this configuration).
My real problem was that the javax.ws.rs.core.Application from the Java ee6 container on SAP Hana Cloud Platform did not work for a unkown reason.
The solution was to download and add the jersey-bundle-1.19.1.jar to WEB-INF/lib and the projects libraries.
There is no problem at all to forward a request from a vanilla servlet to the rest service! If it does not work in your case its most likely your setup or some unexpected reason like it was in my case.
I have a simple controller in a spring boot mvc app packaged as a .war (for aws deployment). I have Thymeleaf on the build path and bootstrap 3.3.6 in resources/static folder. I use #EnableAutoConfiguration in the main.
This controller displays a view just fine.
#Controller
#RequestMapping(value = "/handeware")
public class HomeController {
#RequestMapping(value = "/home", method = RequestMethod.GET)
public String viewHome() {
return "home";
}
}
However, when I remove the #RequestMapping part like this
#Controller
public class HomeController {
#RequestMapping(method = RequestMethod.GET)
public String viewHome() {
return "home";
}
}
It appears that the view gets returned with the Bootstrap CSS styling stripped.
Essentially, I have a domain name www.blah.com with the CNAME pointing to my spring mvc app hosted in aws elastic beanstalk. The idea is that I want someone to be able to type www.blah.com into the browser and be able to see my home page. In order to do this, I removed the #RequestMapping's from my controller, and it works. People can now visit my site at www.blah.com and see my home.html. However, the CSS styling is not showing up now. If I add the #RequestMapping back, the CSS shows back up. Isn't that odd? I confirmed in my local as well as in aws that this is the case. Not sure what to make of it.
How do you reference your css files from your html? You may need to update those references to match the new path.