In Grails 3 I'm trying to use spring-security-oauth, which provides a few endpoints via the #RequestMapping I can see in the mbeans that the path is configured but any request always hits grails and returns a 404.
The requests never seem to hit any of the endpoints configured by the spring-security-oauth lib. Is there anyway to insure the requests hit the endpoints in the jar?
To make sure the endpoints configured by #RequestMapping show in a Grails 3 app using Java config you have to use the following set up in Application.groovy
#ComponentScan("my.org.config")
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application)
}
}
Do not use application.yml in the following way:
grails:
profile: web
spring:
bean:
packages:
- my.org.config
While the beans will be picked up anything with #RequestMapping will not be accessible.
Related
I'm working SOAP and REST together into the same application. Rest web service with #RestController and SOAP with apache cxf.
Rest ws and soap have the same path, for example:
Rest: GET http://localhost:8080/ws/person
SOAP: http://localhost:8080/ws/findPerson
For configuring cxf servlet, i create the following method
#Bean
public ServletRegistrationBean cxfServletRegistration() {
return new ServletRegistrationBean(new CXFServlet(), "/ws/*"); }
SOAP Service are running fine after change but REST (#RestController) stop working, but if I disable the method cxfServletRegistration(), the rest WS working fine.
Could you suggest any solution to make all WS working together ?
You can't, because each servlet must "own" its listening basepath. Despite the lack of an explicit registration, RestControllers listen on a base path (default /*) Do you actually need to use #RestController? CXF has REST support via JAX-RS.
Otherwise, I would suggest to separate your REST and SOAP functionality, such as having REST on /model/... and SOAP on /api/... or some such separation.
I use WildFly 11. I have following #ApplicationPath declaration:
#ApplicationPath("/")
public class JaxrsConfiguration extends Application {}
I have to use root path, but sadly in this configuration RESTEasy matches all requests. Looking at source code at https://github.com/resteasy/Resteasy/blob/master/resteasy-servlet-initializer/src/main/java/org/jboss/resteasy/plugins/servlet/ResteasyServletInitializer.java I found that in my case HttpServlet30Dispatcher is registered at /* which is terrible case. Is it possible to register RESTEasy servlet on "/" or as filter (while still having automatic resource scanning)?
I'm looking at a project with the following Java Configuration file:
#Configuration
#EnableWebMvc
#ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
}
}
The project has some static resources in the webapps/resources directory that are being served up. My question is I'm not sure why. It seems for this to work, the above call to addResourceHandlers(...) should be
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources");
}
I tried to look for something in the Spring documentation possibly indicating default values but could not, so I'm not sure why the project works.
The project can serve static resources thanks to DefaultServletHandlerConfigurer:
Configures a request handler for serving static resources by forwarding the request to the Servlet container's "default" Servlet.
I use Jetty in the following example, but other servlet containers (e.g. Tomcat) should behave similarly. Download the Jetty 9 distribution, then inside webapps, create these files:
example/
hello.txt
Start the Jetty server. We have deployed the simplest application imaginable. You should be able to access the file at:
http://localhost:8080/example/hello.txt
The servlet container can serve static resources without any extra configurations. This behavior is suppressed as soon as Spring MVC comes into play. Spring MVC will create a front controller, DispatcherServlet, and park it at "/". Every incoming request will go through this single servlet, and the servlet will find the appropriate component in the app to actually process the request (e.g. a #RequestMapping method in a #Controller class). If no handlers for the request can be found, we have 404s.
The handler DefaultServletHandlerConfigurer has the lowest precedence. If enabled, it allows the front controller to handle the request first. When that fails, it forwards the request to the servlet container, where the request is treated as a static resource read.
This is a quick and dirty way of serving static files. In practice, you don't want anything off the beaten path to be available via a GET. You want to allow only specific files and folders, and you would use WebMvcConfigurerAdapter.addResourceHandlers().
I'm testing my jax-rs services using aquillian and the wildfly embeded container. In this setup, I'm trying to understand how my web services are handled by the server. To set things up, I have the following deployment in my test:
#Deployment
public static WebArchive create() {
return ShrinkWrap.create(WebArchive.class, "rest-service.war")
.addClasses(ProfileService.class,
Profile.class); // classes and other resources into the war
}
So that war gets deployed and its running in an embedable container. However, my test can't find a service at any of the URLs mapped in ProfileService. Is it because my war is missing a servlet? Specifically, a javax.ws.rs.core.Application instance? Or am I misunderstanding how EJB containers expose jax-rs services.
It is because you're missing a class that extends Application, however you don't need to register it as a servlet. As long as its annotated #ApplicationPath it will be picked up by the container.
This feels like a basic question, but I haven't had much luck Googling.
My app connects to an SMTP server and sends mail through it. I need this SMTP server to be configurable based on which environment the app is deployed to.
How can I specify the specify the SMTP server name in my web.xml config file and access it from my Spring MVC 3.0 controller?
The controller does not extend or implement anything. It is completely annotation driven with #Controller and #RequestMapping. From what I have seen online, people access context-params via the servlet API. Being annotation driven, I do not have access to the servlet object.
I solved this.
Make your controller implement ServletContextAware, which requires a method called
setServletContext(ServletContext servletContext)
Spring MVC will inject the servlet context into this method if your controller is ServletContextAware.
Create a private variable on your controller to store the servletController that is injected into the above method. You can now use servletContext just as you would if you were using a regular servlet.
hth.
Adding an instance of Servletcontext and autowiring it worked for me
#Controller
public MyController {
// other instances relevant to your requirement
#Autowired
private ServletContext sCtx;
//other methods relevant to your requirement
}
I suppose following also should work:
void action(final HttpServletRequest request) {
final paramValue = request.getSession().getServletContext().getInitParameter("paramName");
...
}