I keep getting a NoSuchBeanDefinitionException in my service layer and I have no idea why, any help will be appreciated. This is out of spring in action book, the spitter app. Am i missing something. I am trying to build it myself using maven instead of gradle.
Exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean witlerMapping#0': Initialization of bean failed;
nested exception is org.springfram name 'homeController' defined in file [/data/TOMCAT-6.0/apache-tomcat-6.0.33/wess]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.john.springinaction.service.SpitterService] found for dependency: ndency. Dependency annotations: {};
nested exception is org.springframework.beanspringinaction.service.SpitterService] found for dependency: expected at least 1annotations: {}
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
at org.springframework.beans.factory.support.AbstractAutowireCapableBean
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getOb
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
at org.springframework.context.support.AbstractApplicationContext.finish
at org.springframework.context.support.AbstractApplicationContext.refres
at org.springframework.web.servlet.FrameworkServlet.createWebApplication
at org.springframework.web.servlet.FrameworkServlet.createWebApplication
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationCo
at org.springframework.web.servlet.FrameworkServlet.initServletBean(Fram
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.jav
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:48
at java.lang.Thread.run(Thread.java:619)
Controller:
package com.john.springinaction.mvc;
import java.util.Map;
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.john.springinaction.service.SpitterService;
#Controller
public class HomeController {
public static final int DEFAULT_SPITTLES_PER_PAGE = 25;
#Autowired
private SpitterService spitterService;
#RequestMapping({"/","/home"})
public String showHomePage(Map<String, Object> model) {
model.put("spittles",
spitterService.getRecentSpittles(DEFAULT_SPITTLES_PER_PAGE));
return "home";
}
}
Service:
package com.john.springinaction.service;
import static java.lang.Math.min;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.john.springinaction.domain.Spitter;
import com.john.springinaction.domain.Spittle;
import com.john.springinaction.persistence.SpitterDAO;
#Service("spitterService")
#Transactional(propagation = Propagation.REQUIRED)
public class SpitterServiceImpl implements SpitterService {
#Autowired
private SpitterDAO spitterDAO;
#Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public List<Spittle> getRecentSpittles(int count) {
List<Spittle> recentSpittles =
spitterDAO.getRecentSpittle();
return recentSpittles.subList(0, min(49, recentSpittles.size()));
}
public void saveSpittle(Spittle spittle) {
spittle.setWhen(new Date());
spitterDAO.saveSpittle(spittle);
}
#Transactional(propagation=Propagation.SUPPORTS, readOnly=false)
public void saveSpitter(Spitter spitter) {
if (spitter.getId() == null) {
spitterDAO.addSpitter(spitter);
}else {
spitterDAO.saveSpitter(spitter);
}
}
#Transactional(propagation=Propagation.SUPPORTS, readOnly=false)
public void deleteSpitter(Spitter spitter) {
spitterDAO.deleteSpitter(spitter);
}
#Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public Spitter getSpitter(long id) {
return spitterDAO.getSpitterById(id);
}
public void startFollowing(Spitter follower, Spitter followee) {
}
public List<Spittle> getSpittlesForSpitter(Spitter spitter) {
return spitterDAO.getSpittlesForSpitter(spitter);
}
public List<Spittle> getSpittlesForSpitter(String username) {
return spitterDAO.getSpittlesForSpitter(username);
}
public Spitter getSpitter(String username) {
return spitterDAO.getSpitterByUsername(username);
}
public Spittle getSpittleById(long id) {
return spitterDAO.getSpittleById(id);
}
public void deleteSpittle(long id) {
spitterDAO.deleteSpittle(id);
}
public List<Spitter> getAllSpitters() {
return null;
}
}
beanlocations.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<!-- Database Configuration -->
<import resource="DataSource.xml"/>
<import resource="Hibernate.xml"/>
<import resource="Service.xml"/>
<!-- Auto scan the components -->
<context:component-scan base-package="com.john.springinaction" />
<context:annotation-config/>
</beans>
hibernate.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.john.springinaction.service" />
<tx:annotation-driven/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- context load listener -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- <listener>
<listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
</listener> -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:*BeanLocations.xml
</param-value>
</context-param>
<servlet>
<servlet-name>spitter</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spitter</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.john.springinaction</groupId>
<artifactId>Spitter</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spitter</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.com/maven2/</url>
</repository>
</repositories>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<!-- Spring framework -->
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency> -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<!-- Spring AOP Dependency -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<!-- Spring MVC framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<!-- Hibernate annotation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<!-- Hibernate library dependecy start -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<!-- Hibernate library dependecy end -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.glassfish.v3.osgi</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<outputDirectory>target/artefacts/wars</outputDirectory>
<warName>spitter</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
It seems that you might have SpitterService in different packages. Try to specify which one you want exactly in the controller by either:
#Autowired
#Qualifier( "spitterService" )
private SpitterService spitterService;
or
#Resource( name="spitterService" )
private SpitterService spitterService;
i think u have not written the other necessary classes and interfaces, like Spitter, Spittle, and other service classes please create those classes first
Related
i'm trying to make some REST services with a JBOSS AS7 but when I try to access them the logs says 404.
I been looking for solutions but doesn't found the solution.
The services are in a WAR project, which is in a EAR file to make the deploy in the JBOSS.
I'm using CXF and CDI to make the services.
The JBOSS has the version 3.0.5 of EasyRest.
This is the pom.xml of the WAR project:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>projectParent</groupId>
<artifactId>projectParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>projectParent</groupId>
<artifactId>projectWARModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>projectWARModule Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.apache.cxf.version>3.0.0</org.apache.cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-integration-cdi</artifactId>
<version>${org.apache.cxf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${org.apache.cxf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-wsdl -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-wsdl</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-ws-policy -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.1.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>2.1.2.Final</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<url>http://download.java.net/maven/2/</url>
</repository>
<repository>
<id>jboss-public</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<build>
<finalName>projectWAR</finalName>
</build>
</project>
This is the pom.xml of the EAR package, it have a reference to a JAR because in a tutorial I found about making EAR with maven it says to use it, but doesn't have anything:
<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>projectParent</groupId>
<artifactId>projectParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>projectEARModule</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>projectParent</groupId>
<artifactId>projectJARModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>projectParent</groupId>
<artifactId>projectWARModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<finalName>projectEar</finalName>
</build>
</project>
This class is named PeopleRestService and have the services:
package com.example.rs;
import java.util.Collection;
import javax.inject.Inject;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import com.example.model.Person;
import com.example.services.PeopleService;
#Path( "/people" )
public class PeopleRestService {
#Inject private PeopleService peopleService;
#Produces( { MediaType.APPLICATION_JSON } )
#GET
public Collection< Person > getPeople( #QueryParam( "page") #DefaultValue( "1" ) final int page ) {
return peopleService.getPeople( page, 5 );
}
#Produces( { MediaType.APPLICATION_JSON } )
#Path( "/{email}" )
#GET
public Person getPerson( #PathParam( "email" ) final String email ) {
return peopleService.getByEmail( email );
}
#Produces( { MediaType.APPLICATION_JSON } )
#POST
public Response addPerson( #Context final UriInfo uriInfo,
#FormParam( "email" ) final String email,
#FormParam( "firstName" ) final String firstName,
#FormParam( "lastName" ) final String lastName ) {
final Person person = peopleService.addPerson( email, firstName, lastName );
return Response.created( uriInfo.getRequestUriBuilder().path( email ).build() ).entity( person ).build();
}
#Produces( { MediaType.APPLICATION_JSON } )
#Path( "/{email}" )
#PUT
public Person updatePerson(
#PathParam( "email" ) final String email,
#FormParam( "firstName" ) final String firstName,
#FormParam( "lastName" ) final String lastName ) {
final Person person = peopleService.getByEmail( email );
if( firstName != null ) {
person.setFirstName( firstName );
}
if( lastName != null ) {
person.setLastName( lastName );
}
return person;
}
#Path( "/{email}" )
#DELETE
public Response deletePerson( #PathParam( "email" ) final String email ) {
peopleService.removePerson( email );
return Response.ok().build();
}
}
And this is named JaxRsApiApplication:
package com.example.rs;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
#ApplicationPath( "api" )
public class JaxRsApiApplication extends Application {
}
This is the beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
And this is the web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<servlet>
<servlet-name>PeopleRestService</servlet-name>
<servlet-class>org.apache.cxf.cdi.CXFCdiServlet</servlet-class>
<init-param>
<param-name>org.apache.cxf.cdi</param-name>
<param-value>com.example.rs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PeopleRestService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
When I try run this in a tomcat works perfectly, but in JBOSS only get a 404.
I think could be a problem with the libraries, but don't know what problem especificaly.
If anyone know what could be happening it would be a great help.
Thank you very much and sorry if my english is bad.
Have a nice day.
UPDATE
As #JanezKuhar says I reemplace the imports in the pom. Now the dependencies looks like:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>javax.enterprise</groupId> -->
<!-- <artifactId>cdi-api</artifactId> -->
<!-- <version>1.1</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.apache.cxf</groupId> -->
<!-- <artifactId>cxf-integration-cdi</artifactId> -->
<!-- <version>${org.apache.cxf.version}</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.apache.cxf</groupId> -->
<!-- <artifactId>cxf-rt-frontend-jaxrs</artifactId> -->
<!-- <version>${org.apache.cxf.version}</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>javax.annotation</groupId> -->
<!-- <artifactId>javax.annotation-api</artifactId> -->
<!-- <version>1.2</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.apache.cxf</groupId> -->
<!-- <artifactId>cxf-rt-transports-http</artifactId> -->
<!-- <version>${org.apache.cxf.version}</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.apache.cxf</groupId> -->
<!-- <artifactId>cxf-rt-wsdl</artifactId> -->
<!-- <version>3.0.0</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.apache.cxf</groupId> -->
<!-- <artifactId>cxf-rt-ws-policy</artifactId> -->
<!-- <version>3.0.0</version> -->
<!-- </dependency> -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.jboss.weld.servlet</groupId> -->
<!-- <artifactId>weld-servlet</artifactId> -->
<!-- <version>2.1.2.Final</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.jboss.weld</groupId> -->
<!-- <artifactId>weld-core</artifactId> -->
<!-- <version>2.1.2.Final</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>javax.inject</groupId> -->
<!-- <artifactId>javax.inject</artifactId> -->
<!-- <version>1</version> -->
<!-- </dependency> -->
</dependencies>
But it gives me now the following error:
Caused by: java.lang.ClassNotFoundException: org.apache.cxf.cdi.CXFCdiServlet from [Module "deployment.projectEar.ear.projectWARModule-0.0.1-SNAPSHOT.war:main" from Service Module Loader]
Finaly make this works.
As #JanezKuhar says I comment all the extra libraries in the pom and added two, now looks like this:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>projectParent</groupId>
<artifactId>projectParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>projectParent</groupId>
<artifactId>projectWARModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>projectWARModule Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.apache.cxf.version>3.0.0</org.apache.cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
<version>3.0.0</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.1.2.Final</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<url>http://download.java.net/maven/2/</url>
</repository>
<repository>
<id>jboss-public</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<build>
<finalName>projectWAR</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
<archive>
<manifestEntries>
<Dependencies>org.apache.cxf.impl</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Comment the servlet part and set the restEasy scan true, in the web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
</web-app>
Now it works and I can access the REST servicies deployed in the jboss.
Thanks for the help provided.
Where is your jboss-app.xml? Does maven create it?
In jboss-app.xml you (or maven) should create context root of your application.
jboss-app.xml: Every EAR application should specify a unique string name for the class loader. It's from JBoss doc. link
I get started with a simple web application using the Spring framework and when I trying to run in the IntelliJ, the BUILD is successfull but the app is not getting started, like you see the red lines tell you usually. I get the message provided below:
I have the following web.xml file,
<web-app 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"
version="3.0">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispatcher-servlet.xml file is as following:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="rest.mvc"/>
<mvc:annotation-driven/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
And, here is my project structure:
This is the code for the test controller:
package rest.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by Chaklader on 9/23/16.
*/
#Controller
public class TestController {
#RequestMapping("/test")
public String test(){
return "view";
}
}
What is the issue here and how to solve it?
Note:
<?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>
<packaging>war</packaging>
<groupId>basic-web-app</groupId>
<artifactId>basic-web-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.0.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.12.0.RELEASE</version>
</dependency>
<!-- JSON Processing -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.0</version>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
Try to use below section for tomcat plugin in pom.xml and see if it works
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<ignorePackaging>true</ignorePackaging>
</configuration>
</plugin>
This is my first spring sample project. Its a very basic example where I'm trying to pass firstname and lastname using Ajax to my program. But, its not able to hit the URL. Here is my code. Please let me know if anything is missing.
package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.impl.EmployeeImpl;
import com.model.Employee;
#Controller
public class HelloWorldController {
/*#Autowired
EmployeeImpl employee;*/
#RequestMapping(value = "/hello", method = RequestMethod.POST)
#ResponseBody
public String saveHosting(#RequestBody Employee employee)
{
System.out.println("First name :"+employee.getFirstname());
System.out.println("Last name :"+employee.getLastname());
return "hi";
}
}
My JS code
$(document).ready(function(){
$('#click').click(function() {
var data = {}
data["firstname"] = $('#firstname').val();
data["lastname"] =$('#lastname').val();
console.log("Data :"+data);
$.ajax({
type: "POST",
contentType: "application/json",
url: "/SpringProject/hello/",
data: JSON.stringify(data),
dataType: 'json',
timeout: 600000,
success: function (data) {
alert("success");
},
error: function (e) {
alert("fail");
}
});
});
});
Spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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">
<context:component-scan base-package="com.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- <bean id="employee" class="com.impl.EmployeeImpl">
<property name="firstname" value="shaik"></property>
<property name="lastname" value="ali"></property>
</bean> -->
</beans>
POM.xml
<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>
<groupId>SpringProject</groupId>
<artifactId>SpringProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<junit.version>4.12</junit.version>
<spring.version>4.1.5.RELEASE</spring.version>
<msql.connector.version>5.1.10</msql.connector.version>
<cxf.version>2.2.3</cxf.version>
<log4j.version>1.2.17</log4j.version>
<jackson.version>2.6.3</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${msql.connector.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.12</version>
</dependency>
<!-- <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId>
<version>2.3</version> </dependency> -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
src
maven-compiler-plugin
3.1
1.7
1.7
maven-war-plugin
2.3
WebContent
false
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/lib/soapservice-servlet.xml</param-value>
</context-param> -->
<!-- <listener>
<listener- class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
-->
<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>*.html</url-pattern>
</servlet-mapping>
</web-app>
Error
jquery.min.js:2 POST http://localhost:8080/SpringProject/hello/ 404 (Not Found)
Update your servlet mapping as (in web.xml)
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I checked the headers and made sure I had Accept:application/json
I'm not sure why Spring is still reporting a 406, I can consume MVC controllers fine.
I looked and made sure I had all the right includes in Maven, so that shouldn't be the issue. I'm running Tomcat 8, and Java 8.
Any help would be appreciated!
RestUtilityController
package io.bray.springtest;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.persistence.*;
#RestController
public class RestUtilityController {
private static EntityManager em = Persistence.createEntityManagerFactory("SpringTest").createEntityManager();
#RequestMapping(value="/emailExists", produces="application/json")
#ResponseBody
public Boolean loginConfirmation(#RequestParam("email") String email){
List logins = em.createQuery("SELECT l FROM LoginDto l WHERE l.email = :email").setParameter("email", email).getResultList();
if(logins.size() > 0){
return new Boolean(true);
}
return new Boolean(false);
}
}
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringTest</groupId>
<artifactId>SpringTest</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spring 2 MVC</name>
<properties>
<jdk.version>1.6</jdk.version>
<spring.version>4.2.0.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<servletapi.version>2.5</servletapi.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- Spring MVC framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- for compile only, your container should have this -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servletapi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- embedded jetty, good for testing -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/spring2</contextPath>
</webApp>
</configuration>
</plugin>
<!-- configure Eclipse workspace -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
<wtpContextName>spring2</wtpContextName>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"
xmlns:context = "http://www.springframework.org/schema/context">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:annotation-config/>
<context:component-scan base-package="io.bray.springtest"/>
</beans>
I've seen a lot of this questions in SO and i've tried their solutions but they haven't worked for me so I'll post this one. Basically I'm just setting up a project and trying to get my RequestMapping to work, just to return a string. So this is my code
pom.xml
<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>
<groupId>ve.com</groupId>
<artifactId>mavenDWP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.1.6.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring -->
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
<!-- CGLIB is required to process #Configuration classes -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- Servlet API, JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.5.4</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.10.Final</version>
</dependency>
<!-- JSONDoc -->
<dependency>
<groupId>org.jsondoc</groupId>
<artifactId>jsondoc-core</artifactId>
<version>1.1.15</version>
</dependency>
<dependency>
<groupId>org.jsondoc</groupId>
<artifactId>jsondoc-springmvc</artifactId>
<version>1.1.15</version>
</dependency>
<dependency>
<groupId>org.jsondoc</groupId>
<artifactId>jsondoc-ui-webjar</artifactId>
<version>1.1.15</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
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" version="3.0">
<display-name>AngularSpring</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="ve.com" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="ve.com.HibernateAwareObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="documentationController" class="org.jsondoc.springmvc.controller.JSONDocController">
<constructor-arg name="version" value="1.0"/>
<constructor-arg name="basePath" value="http://localhost:4848/mavenDWP/api"/>
<constructor-arg name="packages">
<list>
<value>ve.com</value>
</list>
</constructor-arg>
</bean>
LinkController
package ve.com.controller;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.jsondoc.core.annotation.ApiVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#Controller
#ApiVersion(since="1.0")
#RequestMapping("/show")
public class LinkController {
#Autowired
private StockService stockService;
#RequestMapping(method = RequestMethod.GET, value="/hello")
public String showHello() {
return "hello";
}
}
Ok Basically that's my code.. Still i browse to http://localhost:4848/mavenDWP/api/show/hello and i just get a 404 error. Any ideas?
Add #ResponseBody annotation. Every time when you return something that is not a build in class such as ModelAndView you need to use this annotation. See docs.
As an advice, try to use a Map even if you have a little amount of data to send to client. A map will be converted to JSON and can be easily accesed.
All is well, Try jsut to remove the space beteween servlet-class and org.springframework.web.servlet.DispatcherServlet in the web.xml
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
Good luck