REST Web-services eclipse - eclipse

I am new to REST Web services and was following a tutorial in Youtube. Although i followed the exact steps, I am unable to successfully run it.
Here is what i did
web.xml file
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<!-- this need same with resteasy servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/resteasy</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/resteasy/*</url-pattern>
</servlet-mapping>
Here is the service class that i created.
#Path("service")
public class Service {
private static Map<Integer , DoctorInformation> doctorInformations = new HashMap<Integer , DoctorInformation>();
static{
Location location = new Location();
location.setCity("Roorkee");
location.setState("Uttarakhand");
DoctorInformation doctorInformation = new DoctorInformation();
doctorInformation.setID(1);
doctorInformation.setFirstName("Sanchit");
doctorInformation.setLastName("Jain");
doctorInformation.setAddressLine1("ABSGDHD");
doctorInformation.setAddressLIne2("NBAJS");
doctorInformation.setPincode(247667);
doctorInformation.setEmail("#gmail.com");
doctorInformation.setSpecialization("ENT");
doctorInformation.setLocation(location);
doctorInformations.put(1 , doctorInformation);
}
//Method that will return single person object in XML
#GET
#Path ("/getPersonByIdXML/{id}")
#Produces(MediaType.APPLICATION_JSON)
public DoctorInformation getPersonByIdXML(#PathParam("id") int id) {
return doctorInformations.get(id);
}
I am using Maven to build the project , and finally when the Web project is deployed in localhost i am using the following url
http://localhost:8080/Project_Name/service/getPersonByIdXML/1
Please let me know what i am doing wrong. I think the <url-patter> in my web.xml is not proper, but when i changed it even the index.html page that loads on http://localhost:8080/Project_Name/ stopped working. In many places i have seen that <init-param> is defined along with ...somthing like this...
init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.package.whatever</param-value>
</init-param>
but in my example i am using org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher as my servlet class. How will it make the difference?
The exception displayed on console is :
SEVERE: failed to execute
javax.ws.rs.NotFoundException: Could not find resource for full path: http://localhost:8080/TestWS/resteasy/service/getPersonByIdXML/1
at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73)
at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:444)
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:234)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:171)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:745)

Related

Spring MVC REST nohandler error

Im new to spring MVC and REST.. I'm having an issue with a simple test controller I've put together from example I've found here and from the spring docs..
When I hit the url http://localhost:8080/test-api/user/14 I get the error below
Im getting the error:
Sep 23, 2015 11:26:55 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/test-api/user/14] in DispatcherServlet with name 'testapi'
Im using xml to config.. Im not ready to move to java config.
web.xml
Spring Web MVC Application
<servlet>
<servlet-name>springtest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springtest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/testapi-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
testapi-servlet.xml - only contains the component scan and annotation driven elements
<context:component-scan base-package="com.springtest.testapi" />
<mvc:annotation-driven />
SpringTest.java
package com.springtest.testapi.api;
#RestController
public class SpringTest {
#RequestMapping(value="/user/{id}", method = RequestMethod.GET)
public User getUser(#PathVariable int id) {
User u = new User(id,"Test","Me");
return u;
}
What handler should I be defining.. None of the examples or docs state that a handler needs to be defined..
Remove the contextConfigLocation.
Replace the following:
<servlet-mapping>
<servlet-name>springtest</servlet-name>
<url-pattern>/test-api</url-pattern>
</servlet-mapping>
Make sure your xml file is test-api-servlet.xml and not testapi-servlet.xml
I found my issue. I mispelled the package in the component scan. The sample code I have was edited so It didn't fully represent what I had and was actually correct.

Cannot create JDBC driver of class '' for connect URL 'null" - HikariCP, Tomcat8, PostgreSQL

Firstly I have never setup a JDBC Pool before (So i am sorry if this seems mundane). I have been trying for some time to solve the problem but to no avail. I have explored the suggested options from various other stackoverflow posts but none have been successful.
I have tried:
Including the 'pgjdbc-ng' driver only in the /lib of tomcat and moving the context to the catalina home conf/
Swapping to using the example given by Hikari (but received same error) https://github.com/brettwooldridge/HikariCP/wiki/JNDI-DataSource-Factory-(Tomcat,-etc.)
I am using HikariCP with a PostgreSQL(pgjdbc-ng) driver. Tomcat8 deploys my war file which i build using maven. web.xml, context.xml, java code.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Restful Web Application</display-name>
<resource-ref>
<res-ref-name>jdbc/postgresHikari</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>Jersey Web Application</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.seng402.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/postgresHikari" auth="Container"
factory="com.zaxxer.hikari.HikariJNDIFactory"
type="javax.sql.DataSource"
minimumIdle="5"
maximumPoolSize="10"
connectionTimeout="300000"
dataSource.implicitCachingEnabled="true"
dataSource.user="docker"
dataSource.password="docker"
jdbcUrl="jdbc:postgresql://192.168.59.103:5432/docker"
driverClassName="com.impossibl.postgres.jdbc.PGDataSource"/>
</Context>
java code
Context initCtx = null;
try {
initCtx = new InitialContext();
Context envCtx;
try {
envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource) envCtx.lookup("jdbc/postgresHikari");
try {
// Allocate and use a connection from the pool
Connection conn = ds.getConnection(); // throws the error
conn.close();
System.out.println("Connected!");
} catch (SQLException e) {
System.out.println("Failed to Connect!");
e.printStackTrace();
}
} catch (NamingException e) {
System.out.println("Failed to Lookup!");
e.printStackTrace();
}
} catch (NamingException e1) {
System.out.println("Fail to get Context!");
e1.printStackTrace();
}
Output:
Failed to Connect!
java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
ds.getConnection() throws the error.
Any suggestions as to how i could fix this or debug further would be greatly appreciated.
SOLVED !!! - context.xml was not being put into the META-INF folder
context.xml was not being put into the META-INF folder
With the JNDI provider, do not use the dataSourceClassName (or dataSource.URL) properties. Use jdbcUrl, and driverClassName if necessary (but try without it first).

java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name [duplicate]

When I try run my project I get error
Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name dispatcher
at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3156)
at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3135)
at org.apache.catalina.startup.ContextConfig.configureContext(ContextConfig.java:1372)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1176)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:305)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more
My web.xml:
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
The <servlet-name> of the <servlet-mapping> entry must be exactly the same as the <servlet-name> of the <servlet> entry.
So, in your specific case, change this line in <servlet-mapping> entry
<servlet-name>dispatcher</servlet-name>
to
<servlet-name>DispatcherServlet</servlet-name>
As written in the
Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name dispatcher
Your servlet name in the <servlet> and <servlet-mapping> must be same.

Mixing REST and JSP in Spring MVC, Cannot find JSP

I'm sure this is a noob question, and I've spent the better part of an hour trawling stackoverflow for an answer but nobody seems to have my case so here we go...
I have a new webapp that uses Spring MVC. Most of the app (99%) is pure REST, so it doesn't have a "view" as such but rather simply sends JSON back down the wire, or sends an alternate HTTP Status for errors etc.
The exception is the login page which needs to be an actual JSP, but somehow the configuration I am using to map my REST controllers is leaving me in a state where normal JSP mappings fail.
Here's what I've got:
In my dispatcher servlet config, the relevant portions are:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
In my attempts to get it working, I have also added a mapping to the "HomeController" which currently just redirects to my login JSP:
<bean name="/" class="com.somepackage.HomeController"/>
Now, in the web.xml I have:
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/*</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/spring-dispatcher-servlet.xml
</param-value>
</context-param>
This works fine for my RESTful controllers, which look like this:
#Controller
#RequestMapping(value = "/api/user")
public class BlahBlahController {...
My "HomeController", which just looks like this:
#Controller
#RequestMapping(value = "/")
public class HomeController extends AbstractController {
#Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("login");
}
}
IS triggered when I hit the "/" url, but I get this error in the logs:
WARNING: No mapping found for HTTP request with URI [/WEB-INF/pages/login.jsp] in DispatcherServlet with name 'spring-dispatcher'
Now I get what it's saying, it doesn't know how to resolve /WEB-INF/pages/login.jsp (this page does exist btw), but I'm stuck as to how I need to alter things to get this to work.
I'm a little confused on how it's supposed to work. Anyone got any clues?
Thanks.
OK.. I found the answer, it's the url-pattern in the dispatcher config.
Instead of:
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
It should be
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I had actually found this answer elsewhere and tried it but "thought" it wasn't working, then realized the reason I thought this was unrelated to the root cause.
No idea why this would work and the other wouldn't.. but one problem at a time...
Put RequestMapping at the method level, I tried at my code and it worked. You don't need to define HomeController bean if you are using #Controller and having a proper "context:component-scan"
#Override
#RequestMapping(value = "/")
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("login");
}
you can also use below code if you just want to redirect a login view for all "/" access.
<mvc:view-controller path="/" view-name="login"/>
Checkout the mvc show case project from github for a helpful reference.

Shiro Intermittent Subject.getPrincipal

I've created a JavaEE6 project and currently I'm using Shiro for authentication and authorization. Using this article as a reference (Using Shiro for Authorization via CDI Interceptors then Easily Test with Arquillian), I've integrated Shiro with CDI. Everything works fine except for sometimes Subject.getPrincipal is null.
Further, investigations shows that sometimes I have at least 2 Subject.getSession().getId().
How I encounter the problem:
Login -> ok with sessionA
Click a link that is secured (pageA) -> ok
Tried to insert a record in the database that failed
Click the same secured link (pageA) -> failed, looking at the trace it produced a different session id sessionB
Refresh and refresh until page (pageA) is ok. Got the same session id during login sessionA.
What could be wrong?
My shiro.ini file
[main]
saltedJdbcRealm=com.sido.commons.web.security.shiro.JdbcRealmImpl
# any object property is automatically configurable in Shiro.ini file
saltedJdbcRealm.jndiDataSourceName=Portal
# the realm should handle also authorization
saltedJdbcRealm.permissionsLookupEnabled=true
# If not filled, subclasses of JdbcRealm assume "select password from users where username = ?"
# first result column is password, second result column is salt
saltedJdbcRealm.authenticationQuery = SELECT password, salt FROM users WHERE username = ?
# If not filled, subclasses of JdbcRealm assume "select role_name from user_roles where username = ?"
saltedJdbcRealm.userRolesQuery = SELECT name FROM roles a INNER JOIN user_roles b ON a.id=b.role_id INNER JOIN users c ON c.id=b.user_id WHERE c.username = ?
# If not filled, subclasses of JdbcRealm assume "select permission from roles_permissions where role_name = ?"
saltedJdbcRealm.permissionsQuery = SELECT action FROM permissions WHERE role = ?
# password hashing specification, put something big for hasIterations
sha256Matcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
sha256Matcher.hashAlgorithmName=SHA-256
sha256Matcher.hashIterations=1
saltedJdbcRealm.credentialsMatcher = $sha256Matcher
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager = $sessionManager
sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
securityManager.sessionManager.sessionDAO = $sessionDAO
cacheManager=org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:shiro-ehcache.xml
securityManager.cacheManager=$cacheManager
shiro.loginUrl = /login.xhtml
[urls]
/login.xhtml = authc
/logout = logout
web.xml
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>south-street</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>home.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
SecurityProducer, a Singleton bean where I instantiate the security manager. It should be available and unique to the entire application right?
#Singleton
public class SecurityProducer {
#Inject
private Logger log;
private SecurityManager securityManager;
#PostConstruct
public void init() {
final String iniFile = "classpath:shiro.ini";
log.debug("Initializing Shiro INI SecurityManager using " + iniFile);
securityManager = new IniSecurityManagerFactory(iniFile).getInstance();
SecurityUtils.setSecurityManager(securityManager);
}
..
}
Binding the SecurityManager nor Subject on initialization (Singleton bean) doesn't fixed the problem.
final String iniFile = "classpath:shiro.ini";
securityManager = new IniSecurityManagerFactory(iniFile).getInstance();
SecurityUtils.setSecurityManager(securityManager);
ThreadContext.bind(SecurityUtils.getSubject()); or ThreadContext.bind(securityManager);
Thanks,
czetsuya
I think I've solved the intermittent session issue by configuring the web.xml base here: http://shiro.apache.org/web.html.
Currently here's how it looks:
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>home.xhtml</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
But is that really the solution? Any ideas?
If you want to build subject manually you should bind it to the thread calling ThreadContext.bind(subject). If you don't do this the next calling SecurityUtils.getSubject() will return the new Subject not one that you've created before.
For example in my application i received session id in request and create a subject from it.
Subject subject = new Subject.Builder().sessionId(sessionId).buildSubject();
ThreadContext.bind(subject);