Implement spring security on google app engine - spring

I'm trying to integrate the spring security on google app engine. But it doesn't work properly. I wang to authenticate user when they try to access index page, and redirect them to login page. But now I can visit the index page directly.
I followed spring.io website tutorial and mkyong tutorial.
here are part of my pom.xml dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
AppConfig class
#EnableWebMvc
#Configuration
//#ComponentScan({ "com.example.web.*" })
#ComponentScan({ "com.example.web" })
#Import({ SecurityConfig.class })
public class AppConfig {
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
SecurityConfig class
#Configuration
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password")
.roles("USER");
}
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().formLogin()
.loginPage("/account/login");
}
}
SecurityWebApplicationInitializer class
public class SecurityWebApplicationInitializer extends
AbstractSecurityWebApplicationInitializer {
}
WebApplicationInitializer class
public class WebApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
AccountController class
#Controller
#RequestMapping("/account")
public class AccountController {
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String Index(Model model) {
return "login";
}
}
HomeController class
#Controller
#RequestMapping("/")
public class HomeController {
#RequestMapping(method = RequestMethod.GET)
public String Index(Model model) {
model.addAttribute("x", 1);
model.addAttribute("y", 2);
model.addAttribute("z", 3);
return "index";
}
index.jsp page
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%#page session="true"%>
<!DOCTYPE html>
.....
login.jsp page
<%#page session="false"%>
<!DOCTYPE html>
What I want to achieve now is to redirect unauthenticated user to login page.
But now it does not work, I can visit home page directly.

WebApplicationInitializer requires Servlet 3.0, but Appengine supports only Servlet 2.5. So you have to use plain XML based config, at least for initialization. And configure Spring filter/servlet in web.xml manually.
You need to put into web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>path.to.AppConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
and into spring-security.xml:
<context:annotation-config/>
<beans:bean class="path.to.SecurityConfig"/>
Basically it's all standard stuff from pre-servlet 3.0 time, and you could use any tutorial (or old docs) based on servlet 2.4 or 2.5, it will work on Appengine.
PS also you could vote for Servlet 3.0 support at https://code.google.com/p/googleappengine/issues/detail?id=3091

Related

#RequestMapping does not redirect me to my url

I am entering Spring MVC but it does not redirect me to the url with the RequestMappin, it only does it with the main page but with the others if I add it manual it does not execute, what can I have wrong?
the url that I use to enter login is http://localhost:8080/spring-mvc/login
#WebServlet(urlPatterns = "/login.do") <------------------This work
public class LoginServlet extends HttpServlet {
private LoginService service = new LoginService();
#Controller
public class LoginController { <!---------------------------This not-->
#RequestMapping(value="/login")
#ResponseBody
public String decirHola() {
return "Hola a todos";
}
<welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>
<servlet>
`<servlet-name>dispatcher</servlet-name>`
` <servlet-class>`
` org.springframework.web.servlet.DispatcherServlet`
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/todo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
``` <servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc/*</url-pattern>
</servlet-mapping>
<!-- pom -->
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>`

How to make this spring-boot code work on weblogic 10.3.6?

I am using spring boot 1.5.0 with Java 7 and trying to deploy my code on weblogic 10.3.6 but I am unable to deploy it and giving some errors. I am also using oauth2 for authentication
my classes :
1) UserController.java
#RestController
#RequestMapping("user")
public class UserController {
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
#Autowired
private UserService userService;
#RequestMapping("/all")
public WebAPIResponse<User> getAll(){
System.out.println("inside user all");
LOGGER.info("inside /user/all endpoint");
return userService.getAll();
}
}
2) UserRepository.java
#Repository(value = "userRepo")
public interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {
}
3) UserRepositoryCustom.java
public interface UserRepositoryCustom {
User getByUsername(String username) throws Exception;
}
4) UserRepositoryCustomImpl.java
#Repository
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
#PersistenceContext
EntityManager entityManager;
#Override
public User getByUsername(String username) throws Exception {
Query query = entityManager.createQuery("select u from User u where u.username = :username");
query.setParameter("username", username);
return (User) query.getSingleResult();
}
}
5) UserService.java
public interface UserService {
WebAPIResponse<User> getByUsername(String username);
}
skipping implementation of UserService here
OAuth Classes:
1) AuthorizationServerConfiguration.java
#Configuration
#EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
private static String REALM="MY_OAUTH_REALM";
#Autowired
private TokenStore tokenStore;
#Autowired
private UserApprovalHandler userApprovalHandler;
#Autowired
#Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
#Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
int accessTokenMinutesValidity = 60;
int refreshTokenMinutesValidity = 24 * 60;
clients.inMemory()
.withClient("my-trusted-client")
.authorizedGrantTypes("client_credentials","password", "refresh_token")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.secret("secret")
.accessTokenValiditySeconds(60 * accessTokenMinutesValidity ).//Access token is only valid for 60 minutes.
refreshTokenValiditySeconds(60 * refreshTokenMinutesValidity);//Refresh token is only valid for 24 hours
}
#Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
.authenticationManager(authenticationManager);
}
#Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer
.allowFormAuthenticationForClients()
.realm(REALM+"/client");
}
}
2) MethodSecurityConfig.java
#Configuration
#EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
#Autowired
private OAuth2SecurityConfiguration securityConfig;
#Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
3) OAuth2SecurityConfiguration.java
#SuppressWarnings("deprecation")
#Configuration
#EnableWebSecurity
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
#Autowired
private ClientDetailsService clientDetailsService;
#Autowired
private DataSource dataSource;
#Primary
#Bean
public DataSource customDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
//datasource settings here
return dataSource;
}
#Bean
#ConfigurationProperties("spring.datasource")
public DataSource ds() {
//return DataSourceBuilder.create().build();
return customDataSource();
}
//USERS FROM DATABASE
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
{
PasswordEncoder encoder = NoOpPasswordEncoder.getInstance();
BCryptPasswordEncoder enc;
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select USERNAME, ENC_PASSWD as PASSWORD, IS_ACTIVE AS ENABLED FROM USER_MSTR WHERE USERNAME=?")
.authoritiesByUsernameQuery("select USERNAME, 'ROLE_CLIENT' as ROLE from USER_MSTR where USERNAME=?")
.passwordEncoder(NoOpPasswordEncoder.getInstance())
;
}
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.anonymous().disable()
.authorizeRequests()
.antMatchers("/oauth/token").permitAll();
}
#Override
#Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
#Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
#Bean
#Autowired
public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
handler.setTokenStore(tokenStore);
handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
handler.setClientDetailsService(clientDetailsService);
return handler;
}
#Bean
#Autowired
public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
TokenApprovalStore store = new TokenApprovalStore();
store.setTokenStore(tokenStore);
return store;
}
}
4) ResourceServerConfigurerAdapter.java
#Configuration
#EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
private static final String RESOURCE_ID = "my_rest_api";
#Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(RESOURCE_ID).stateless(false);
}
#Override
public void configure(HttpSecurity http) throws Exception {
http.
anonymous().disable()
.requestMatchers()
.antMatchers("/category_mstr/**", "/equipment/**", "/param_mstr/**", "/chklist_txn/**", "/settings/**", "/user/**")
.and().authorizeRequests()
.antMatchers("/category_mstr/**", "/equipment/**", "/param_mstr/**", "/chklist_txn/**", "/settings/**", "/user/**")
.access("hasRole('ROLE_CLIENT')")
.and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
}
Also attaching my config files here :
1) weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
<wls:context-root>IndogulfAPI</wls:context-root>
<wls:weblogic-version>10.3.6</wls:weblogic-version>
<wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
</wls:container-descriptor>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
2) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.grasim.IndogulfAPI.IndogulfApiApplication</param-value>
</context-param>
<listener>
<listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>metricFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>metricFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
EDIT -----
pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.grasim</groupId>
<artifactId>IndogulfAPI</artifactId>
<!-- <version>0.0.1-SNAPSHOT</version> -->
<packaging>war</packaging>
<name>IndogulfAPI</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.7</java.version>
<springsecurity.version>4.1.1.RELEASE</springsecurity.version>
<springsecurityoauth2.version>2.0.12.RELEASE</springsecurityoauth2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-tomcat</artifactId> -->
<!-- <scope>provided</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- HikariCP connection pool -->
<!-- c#11 -->
<!-- <dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.0</version>
</dependency> -->
<!-- Spring Security -->
<!-- <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${springsecurity.version}</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Spring Security OAuth2-->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${springsecurityoauth2.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc7 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
why do you have web.xml in spring-boot application, and you should use just spring not spring boot if you want to deploy in weblogic container, also can you please add your pom.xml or gradle file as well.

Handling Post requests in Jersey/GWT application

I'm trying to add some REST endpoints to Webprotege project, GWT based, using a Jersey based servlet (which I'm learning now).
Adding some Maven dependencies and the servlet declaration in the web.xml file, I got it working.
My iussue is now hanling POST request, since the incoming http body is not unmarshalled in the class which maps it.
GET requests work.
Here's my dummy class which will containt all RESTFul controller:
#Path("hellos")
public class XlodAPIServiceImpl {
#XmlRootElement
public class Request{
#XmlElement
private String message;
public Request(String message) {
super();
this.message = message;
}
public Request(){}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
#POST
#Produces("application/json")
#Consumes("application/json")
public Collection<String> get(Request request) {
try {
return new ArrayList<String>();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return new ArrayList<String>();
}
}
Server got me always 500, without any stacktrace/useful tip.
The iussue may be related to JSON unmarshalling, since also GET requests with JSON responses set work as expected.
This is what I added in the pom.xml file:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.2</version>
</dependency>
while here's the web.xml:
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
edu.stanford.bmir.protege.web.server
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Suggestions?
Thanks in advance,
FB

Autowired Object is coming out null for a web project

I am always getting autowired object null, please help. From logs beans were initialized properly.
===================================================================================
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RestfulService</display-name>
<!-- <welcome-file-list>
<welcome-file>Readme.html</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> -->
<servlet>
<servlet-name>Jersey REST Service</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.restfulservice.gateway</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
==========================================================================================
applicationcontext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- <bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> -->
<context:component-scan base-package="com.restfulservice" />
<context:annotation-config/>
<!-- <context:property-placeholder location="classpath:testme.properties" /> -->
<bean id="incomingCall" class="com.restfulservice.gateway.IncomingCall" />
<bean id="testPropertyRead1" class="com.restfulservice.gateway.TestPropertyRead">
<property name="name" value="${name}"/>
<property name="address" value="${address}"/>
</beans>
===========================================================================================
incomingcall.java
package com.restfulservice.gateway;
import java.io.File;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
//#Path("/v1/status/*")
#Path("/v1status")
#Component
public class IncomingCall {
final static Logger logger = Logger.getLogger(IncomingCall.class);
public static final String Version = "0.0.1";
#Autowired
#Qualifier("testPropertyRead1")
private TestPropertyRead testPropertyRead;
public TestPropertyRead getTestPropertyRead() {
return testPropertyRead;
}
public void setTestPropertyRead(TestPropertyRead testPropertyRead) {
this.testPropertyRead = testPropertyRead;
}
#GET
#Produces(MediaType.TEXT_HTML)
public String returnTitle()
{
logger.debug("inside returnTitle function call");
return "<p> Java Rest Web Service JSB ....</p>";
}
#Path("/version")
#GET
#Produces(MediaType.TEXT_HTML)
public String returnVersion()
{
return "<p> Java Rest Web Service JSB ....</p>" + "Version:" + Version;
}
#Path("/testspring")
#GET
#Produces(MediaType.TEXT_HTML)
public String testSpring()
{
logger.debug("inside testSpring function call");
logger.info("sai baba is awesome he is making me work, he is my energy");
ApplicationContext context = new ClassPathXmlApplicationContext(
"SpringBeans.xml");
TestSpringSimple obj = (TestSpringSimple) context.getBean("testsprings1");
obj.printHello();
logger.info("going to call callhibernate");
obj.callHibernate();
logger.info("called callhibernate");
return "<p> Java Rest Web Service JSB ...testing spring</p>";
}
#Path("/testconfigreader")
#GET
#Produces(MediaType.TEXT_HTML)
public String testConfigReader()
{
logger.debug("inside testConfigReader function call");
/*ApplicationContext context = new ClassPathXmlApplicationContext(
"ApplicationContext.xml");
TestPropertyRead obj = (TestPropertyRead) context.getBean("testconfig");
*/
//TestPropertyRead testconfig = (TestPropertyRead) TestPropertyRead.getBean("testconfig");
return "<p> Java Rest Web Service JSB ...testing spring config reader "+ testPropertyRead.printAll()
+ "</p>";
//return "<p> Java Rest Web Service JSB ...testing spring config reader "+ ((TestPropertyRead)TestPropertyRead.getBean("testconfig")).printAll()+ "</p>";
}
/**
* Similarly one can download text data also
* #return
*/
#Path("/downloadimage")
#GET
#Produces("image/png")
public Response downloadimage()
{
logger.debug("inside downloadimage function call");
File file = new File("C:\\swamishiva.jpg");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition",
"attachment; filename=image_from_server.png");
return response.build();
}
}
===============================================================================
testpropertyRead.java
package com.restfulservice.gateway;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
#Component
public class TestPropertyRead
{
//private static ApplicationContext CONTEXT;
private String name;
private String address;
public TestPropertyRead()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String printAll() {
System.out.println("name:" + name + " Address:" + address);
return "name:" + name + " Address:" + address;
}
}
I am pretty new to spring but generally find that when I have null pointers for Autowired properties it is because the component scan is failing.
Perhaps have a look at this question Spring Boot MVC and check if anything helps?
One I always forget about is #EnableAutoConfiguration.
Pom Need support of rest and spring integration:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.4.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.4.1</version>
</dependency>

Multiple resouceBean configuration in CXF using Spring

I'm using CXF RS 2.5.1 with Spring 3.0.6-RELEASE. I would like to have multiple implementation classes for "a single endpoint". I see that this issue was reported and fixed https://issues.apache.org/jira/browse/CXF-2439, however, when I try to do it, CXF just selects the first resource class from jaxrs:serviceBeans tag. For the other request, I see this message on tomcat console as "No operation matching request path /account/rest/transfer is found". Below is the configuration. Appreciate any input.
web.xml
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:account-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXF Servlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
account-servlet.xml
<jaxrs:server id="accountService" address="/rest">
<jaxrs:serviceBeans>
<ref bean="transferService" />
<ref bean="balanceService"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
</jaxrs:server>
<bean id="transferService" class="com.mycompany.service.TransferService"/>
<bean id="balanceService" class="com.mycompany.service.BalanceService"/>
BalanceService.java (imports omitted)
package com.mycompany.service;
#Path("/")
#Produces("application/xml")
public class BalanceService{
#GET
#Path("/balance")
public String getBalance() {
StringBuilder response = new StringBuilder(128);
response.append("<Balance>")
.append("<amount>").append("250.00").append("</amount>")
.append("</Balance>");
return response.toString();
}
}
TransferService.java (imports omitted)
package com.mycompany.service;
#Path("/")
#Produces("application/xml")
public class TransferService {
#GET
#Path("/transfer")
public String getTransfer() {
StringBuilder response = new StringBuilder(128);
response.append("<Transfer>")
.append("<amount>").append("350.00").append("</amount>")
.append("</Transfer>");
return response.toString();
}
}
Please ignore any programming irregularities/standards as it's just a sample app for the POC.
I solved this problem by moving part of the #Path mapping to the service bean class. In your case:
BalanceService
#Path("/balance")
#Produces("application/xml")
public class BalanceService {
#GET
#Path("/{id}")
public String getBalance(#PathParam("id") long id) {
...
}
}
TransferService
#Path("/transfer")
#Produces("application/xml")
public class TransferService {
#GET
#Path("/{id}")
public String getTransfer(#PathParam("id") long id) {
...
}
}
So I spend some time searching the internet but found no solution to this problem. There is a note written in the documentation that can be used to deduce the solution.
http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Customselectionbetweenmultipleresources
Hence I wrote a custom resource comparator, did the appropriate jaxrs:server configuration and Eureka! it worked!. Now, I have 2 implementation classes mapped to a single resource/address in javax:rs address.
Please be advised that logic in custom resource comparator shown below may vary based on the URL pattern.
Providing source of all the files. Hope that this will help someone in future :)
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/account-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXF Servlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXF Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
account-servlet.xml (applicationContext)
<beans>
<jaxrs:server id="accountService" address="/rest">
<jaxrs:serviceBeans>
<ref bean="accountServiceImpl" />
<ref bean="transferServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:resourceComparator>
<bean id="accountServiceComparator" class="com.etrade.comparator.AccountServiceComparator"/>
</jaxrs:resourceComparator>
<jaxrs:extensionMappings>
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
</jaxrs:server>
<bean id="accountServiceImpl" class="com.etrade.service.AccountService" />
<bean id="transferServiceImpl" class="com.etrade.service.TransferService" />
</beans>
pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>cxf.rest</groupId>
<artifactId>account</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>account Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- This plugin is needed for the servlet example -->
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.2.0.v20101020</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution><goals><goal>java</goal></goals></execution>
</executions>
</plugin>
</plugins>
<finalName>account</finalName>
</build>
</project>
Custom Resource comparator
package com.etrade.comparator;
import java.lang.reflect.Method;
import javax.ws.rs.Path;
import org.apache.cxf.jaxrs.ext.ResourceComparator;
import org.apache.cxf.jaxrs.impl.UriInfoImpl;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.message.Message;
public class AccountServiceComparator implements ResourceComparator{
public int compare(ClassResourceInfo arg0, ClassResourceInfo arg1,
Message message) {
UriInfoImpl uriInfo = new UriInfoImpl(message);
String path = uriInfo.getPath();
String[] pathArray = path.split("/");
String resourceUrlName = pathArray[1];
System.out.println("Path : "+resourceUrlName);
Method[] methods = arg0.getServiceClass().getMethods();
int value = 1;
String resource = null;
for(Method method : methods) {
Path annotationPath = method.getAnnotation(javax.ws.rs.Path.class);
if(null != annotationPath){
String pathValue = annotationPath.value();
String[] parts = pathValue.split("/");
resource = parts[1];
System.out.println("resource : "+resource);
}
if(resourceUrlName.contains(resource)){
value = -1;
}
}
return value;
}
public int compare(OperationResourceInfo arg0, OperationResourceInfo arg1,
Message arg2) {
return 0;
}
}
Implementation classes/beans
AccountService.java
package com.etrade.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
#Path("/account")
#Produces("application/xml")
public class AccountService {
#GET
#Produces("application/xml")
#Path("/balance/{id}")
public String accountBalance(#PathParam("id") long id) {
System.out.println("id : "+id);
StringBuilder response = new StringBuilder(256);
response.append("<Response>").append("<id>").append(id)
.append("</id>").append("<balance>").append("250.00").append("</balance>")
.append("</Response>");
return response.toString();
}
}
TransferService.java
package com.etrade.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
#Path("/account")
#Produces("application/xml")
public class TransferService {
#GET
#Produces("application/xml")
#Path("/transfer/{id}")
public String accountTransfer(#PathParam("id") long id) {
System.out.println("transfer id : "+id);
StringBuilder response = new StringBuilder(256);
response.append("<Response>").append("<id>").append(id)
.append("</id>").append("<transfer>").append("250.00").append("</transfer>")
.append("</Response>");
return response.toString();
}
}
URLs:
http://localhost:8080/rest/account/balance/12
http://localhost:8080/rest/transfer/balance/13
You can probably simplify the logic to do with comparing the root resources, example, knowing the request URI and the name of the root resource class can be sufficient to make the decision, checking the individual methods looks complicated. BTW, the custom resource comparators are only needed when either root resources and/or individual resource methods can become the equal candidates
I'd solve the problem this way:
Removing a "Custom Resource Comparator" (there's no need to)
Removing this:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.5.0</version>
</dependency>
In the account-servlet.xml/applicationContext:
<beans>
<jaxrs:server id="accountService" address="/rest">
<jaxrs:serviceBeans>
<ref bean="accountServiceImpl" />
<ref bean="transferServiceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
In the beans/class implementation:
...
#Context("accountServiceImpl")
#Path("/account")
public class Accountservice{
...
And that's all. :)

Resources