I have developed a simple RESTful service using Apache CXF coupled in a web application and is working fine.
I Can access it on "http://localhost:8080/SpringRestProjectJava/api/books/1234" and I am also getting the proper JSON response.
My understanding is that from this link that WADL will be autogenerated.
Is it correct? If yes, how can I see WADL for this service.
This is my web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/CustomSpringConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>simplerest</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>org.gsdev.ws.bookservice.BookResource</param-value>
</init-param>
<init-param>
<param-name>jaxrs.providers</param-name>
<param-value>org.gsdev.ws.bookservice.provider.XstreamJsonProvider</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simplerest</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
This is my BookResource.java
#Path( "books/{isbn}" )
public class BookResource {
#GET
#Produces ( "application/json" )
public Book getDetails( #PathParam("isbn") String isbn){
if( isbn.equals( "1234" )){
Book book = new Book();
book.setIsbn(isbn);
book.setTitle("Learning web services by Garry");
return book;
}
return null;
}
}
Finally, after keep on trying I am able to make it work. I think its due to the CXFNonSpringJaxrsServlet. I made below changes and I was able to access the autogenerated WADL.
Changes to web.xml
<servlet>
<servlet-name>simplerest</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simplerest</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Added cxf-servlet.xml in WEB-INF
<jaxrs:server id="bookService" address="/bookservice">
<jaxrs:serviceBeans>
<ref bean="bs"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean='jsonProvider' />
</jaxrs:providers>
</jaxrs:server>
<bean id="bs" class="org.gsdev.ws.bookservice.BookResource"/>
<bean id="jsonProvider" class="org.gsdev.ws.bookservice.provider.XstreamJsonProvider"/>
Have you tried:
http://localhost:8080/SpringRestProjectJava/api/books?_WADL
By the way lots of goodies here:
http://cxf.apache.org/docs/jax-rs.html
Related
I am trying to use Jersey 2.x and have a servlet call "myapp", configuration on web.xml is as follows:
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.private.myapp.resource
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
and have a servlet mapping as follows
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/instance/create</url-pattern>
<url-pattern>/instance/list</url-pattern>
</servlet-mapping>
when I request to $SERVER_ROOT/instance/create or $SERVER_ROOT/instance/list its return 404
but when I change servlet mapping as follows
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
then requesting to $SERVER_ROOT/instance/create or $SERVER_ROOT/instance/list response as expected
can anyone tell what I am missing?
I have made a small spring mvc app. In my controller i have 2 methods which returns the names of the jsp files in the web-inf folder.
Now the application works perfectly, but if i try to add a url path it doesn't work.
What i mean is something like this:
#Controller
#RequestMapping("/start") //if add this it doesn't work
public class SalesController {
#RequestMapping(value = "/greeting")
public String sayGreeting(Model model) {
model.addAttribute("greetingMsg", "Hello Spring");
return "welcome";
}
#RequestMapping(value = "/hello")
public String getHello(Model model) {
model.addAttribute("greeting", "Yo man");
return "hello";
}
}
Here is my servletConfig configuration
<mvc:annotation-driven />
<context:component-scan base-package="com.myCompany" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
If i give the path "myApplicationName"/start/greeting it give error. But if i remove start it works.
What seems to be the problem here?
Thank you
Update:
Below is my web.xml configuration
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servletConfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Add /.html* in your URL Pattern:
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servletConfig.xml</param-value>
</init-param> </servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>/*.html</url-pattern> </servlet-mapping>
I have a Jersey / Spring REST servlet. I am trying to use URL versioning mechanism to have 2 versions of the same resource.
What is the best way to solve this ?
This is my web.xml I am trying to load up 2 jersey servlets
<servlet>
<servlet-name>REST_V1</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comp.resource.v1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST_V1</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
This is the V2 mapping
<servlet>
<servlet-name>REST_V2</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comp.resource.v2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST_V2</servlet-name>
<url-pattern>/v2/*</url-pattern>
</servlet-mapping>
I have defined 2 spring components, with the same resource path in their respective packages
package com.comp.resource.v1;
#Controller
#Path("/user")
public class User_V1 {
}
For V2
package com.comp.resource.v2;
#Controller
#Path("/user")
public class User_V2 {
}
I am seeing a conflicting URI Template error for the resource /user
Is there a better way to solve this ? Any help wold be appreciated
Seems like the issue is with how spring beans are loaded. In the web.xml if you have contextConfigLocation outside of the Jersey to load up all the beans, then both REST_V1 and REST_V2 servlet conflicts with the same resource name.
Here is what I changed in the application context.
Removed scanning of resource package from global applicationContext.xml
<context:annotation-config />
<context:component-scan base-package="com.comp.*">
<context:exclude-filter type="regex" expression="com.comp.resource.*"/>
</context:component-scan>
Added 2 more applicationContext, for each servlet
applicationContext_V1.xml
<context:annotation-config />
<context:component-scan base-package="com.comp.resource.v1"/>
applicationContext_V2.xml
<context:annotation-config />
<context:component-scan base-package="com.comp.resource.v2"/>
Added reference to these applicationContext file in jersey configuration in web.xml
<servlet>
<servlet-name>REST_V1</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comp.resource.v1</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_V1.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST_V1</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
and for REST_V2
<servlet>
<servlet-name>REST_V2</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comp.resource.v2</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_V2.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST_V2</servlet-name>
<url-pattern>/v2/*</url-pattern>
</servlet-mapping>
how to change locale in filter class in springMVC.
in my filter class I'm using below code:
LocaleContextHolder.setLocale(new Locale(lang));
I'm passing lang value is "ms".
when I change locale from UI it is working fine. i want change in filter calss, it is not working.
in my spring-servlet.xml below things i configured.
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName">
<value>locale</value>
</property>
</bean>
</mvc:interceptors>
Web.xml
org.springframework.web.context.ContextLoaderListener
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/conf/context/spring-platform.xml
classpath:/conf/context/spring-beans.xml
classpath:/conf/context/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>accessFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>accessFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<servlet>
<servlet-name>jersy</servlet-name>
<!-- <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> -->
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<!-- <init-param> <param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.managers</param-value> </init-param> -->
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
<!-- <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>org.filter.ServiceRequestFilter</param-value> -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersy</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>http://www.springframework.org/tags</taglib-uri>
<taglib-location>/resources/tlds/spring.tld</taglib-location>
</taglib>
</jsp-config>
Noting is happening(Locale is not changing). No Exception in logs or console.
You are using SessionLocaleResolver so please try with below code.
Locale locale = new Locale(lang);
WebUtils.setSessionAttribute(httpRequest,SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
it will set you locale into session. I think it will resolve your problem .
I have got a really basic and important question to you: My CXFServlet which is controlled by an EmbeddedTomcat deploys the webservices, when I call the url to my servlet.
How can I change that? Are there any solutions?
Use <load-on-startup>1</load-on-startup> in web.xml:
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:cxf-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>