Spring MVC Files org.springframework.web.servlet.DispatcherServlet noHandlerFound - spring

I have tried multiple times, but still I am getting these issue, but unable to understand from where this is arising.
Oct 2, 2013 1:50:37 PM
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI
[/Demo/greeting.html] in DispatcherServlet with name 'demoServlet'
My web.xml
<servlet>
<servlet-name>demoServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>demoServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
And My servlet-config.xml
<mvc:annotation-driven />
<context:component-scan base-package="com.demo.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/content/" p:suffix=".jsp"></bean>
My HelloController
#Controller
public class HelloController {
#RequestMapping(value ="/greeting")
public String sayHello(Model model){
model.addAttribute("greeting","Hello World !");
return "hello";
}
}

I have no idea but this solved my problem!! [Would Like to know the reason for the original problem]
I changed
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
to
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
and it ran perfectly fine :)

Try to replace:
#RequestMapping(value ="/greeting")
with:
#RequestMapping(value ="/greeting.html")

Related

OpenAPI add server at Server Start

I try to modify server list (or default server) on Swagger UI.
1/ On Spring (Not Spring Boot) configuration class I add this :
#Bean
public OpenAPI customOpenAPI() {
log.info("<<<customOpenAPI>>>");
Server server = new Server();
server.setUrl("http://localhost:8091/eatery_spring_rs/rs/");
return new OpenAPI()
.servers(List.of(server));
}
2/ But I never obtain result on Swagger UI, no server is added :
3/ Maven dependencies are :
<!--SWAGGER-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.7</version>
</dependency>
<!-- END -->
5/ Question :
Is it possible to change context path, or add server URL, by program ?
6/ I can affine the description of problem
The context path of application is not complete
It is a Spring 5.3 application, start with web.xml in App Server.
like Tomcat, ...
This is the web.xml definition :
`
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-service.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>`
This mean that the real context path is : [app.context]/rs but in the case of :
#Bean public Docket api(ServletContext servletContext) {
servletContext is equal to [app.context] NOT to [app.context]/rs
It's help ?

#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>`

Multiple Contexts For Request Scope?

I am attempting to use Weld with Tomcat7 and Jersey, and in my log files I am seeing this:
org.jboss.weld.exceptions.IllegalStateException: WELD-001304: More than one context active for scope type javax.enterprise.context.RequestScoped
The application deploys properly and I only see this when attempting to hit on my Jersey endpoints.
Here is the pertinent parts of the pom.xml
<!-- Jersey Deps -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<!-- CDI Deps -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.3.2.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
</dependency>
And here's my "MainApplication" that extends ResourceConfig:
public class MainApplication extends ResourceConfig {
public MainApplication() {
packages(true, "com.example.api");
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
}
}
Here's my web.xml (the servlet section):
<servlet>
<servlet-name>Jersey Servlet Container</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>net.di2e.isfr.foldr.MainApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Servlet Container</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
I am uber-confsued what could be going wrong.

Access to the specified resource has been forbidden in simple Spring webservice Demo

I have created simple spring security demo with REST webservice.
I have spent lot of hours on It.Need strong pointers regarding simple working spring security for rest webservice with latest versions.
My Controller is
#Controller
public class RestContoller {
#RequestMapping(value = "/countryJSONProduce", method = RequestMethod.GET)
public ResponseEntity<CountryDetail> getCountryJSON() {
CountryDetail countryDetail = new CountryDetail("Values");
ResponseEntity<CountryDetail> rentity = new ResponseEntity<CountryDetail>(countryDetail, HttpStatus.OK);
return rentity;
}
#RequestMapping(value = "/countryJSONConsume", consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public String consumeJSON(#RequestBody CountryDetail countryDetail) {
System.out.println("Country Detail Example");
return "home";
}
}
web.xml is
<servlet>
<servlet-name>springrest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springrest</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml
/WEB-INF/springrest-servlet.xml
</param-value>
</context-param>
my pom.xml is
<properties>
<spring.version>4.1.0.RELEASE</spring.version>
<springsecurity.version>4.0.2.RELEASE</springsecurity.version>
</properties>
<dependencies>
<!-- for Jsp use -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- Spring mvc and Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring mvc and Core -->
<!-- JSON Response Spring Framework 4.1, the minimum jackson version should
be 2.1 -->
<!-- Compatiable Spring Framework 4.1 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<!-- Spring Authentication Start -->
<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>
<!-- Spring Authentication End -->
</dependencies>
When I Run the code sometimes it shows popup in my Eclipse Mars INTENAL
browser for user and password.when i put my credentials it will goes to
localhost:8080/SpringMavenRest2/ welcome page ok.when I hit the
url localhost:8080/SpringMavenRest2/hello/countryJSONProduce
which is calling my first service.. it is showing the Error :Access to
the specified resource has been forbidden.403
Even I put user name and
password as basic auth.Now I am testing this second url FROM CHROME
POSTMAN CLIENT.
I am using this configuration Java 1.8 ,Tomcat 8.0
spring.version4.1.0.RELEASE ,springsecurity.version 4.0.2.RELEASE.
and maven 3.3
Its
working well without authentication.Could you give any best referenced demo
for spring security with basic authentication. I have refered this also
http://www.mkyong.com/spring-security/spring-security-hello-world-example/
Try this :
<security:intercept-url pattern="/hello/**" access="hasRole('ROLE_USER'') "/>
and / or
<security:intercept-url pattern="/**" access="hasAnyRole('IS_AUTHENTICATED_ANONYMOUSLY','ROLE_USER')"/>

ResourceConfig instance does not contain any root resource classes

I can't seem to get my jersey resources recognized in my app. According to everything I read online, there are two possible cases where this may happen.
There is no valid package declared in the web.xml under com.sun.jersey.config.property.packages.
This is my web.xml:
<web-app version="3.0"
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_3_0.xsd"
metadata-complete="false">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>my.package</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
:
2. There is no valid resource class in my.package.
My resource class:
package my.package;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
#Path("/")
class MyClass{
#GET
public Response asdf(){
return Response.ok().build();
}
#Path("/test")
#GET
public Response test(){
return Response.ok().build();
}
}
As far as I can tell, neither of these two conditions are satisfied.
For the sake of completion, here are my dependencies:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17</version>
</dependency>
Any ideas as to why I might be getting this error?
#Path("/")
class MyClass{
The class isn't visible to Jersey. Try changing it to (note the addition of "public"):
#Path("/")
public class MyClass{

Resources