Spring REST maven项目配置&运行

时间:2017-03-22 13:56:58

标签: java spring rest maven spring-mvc

我是Java / Spring / Rest /...

的初学者

我有以下项目,我尝试开发一个可以访问Oracle数据库的rest ws。 对于这个项目,我使用的是最新版本的Spring和JPA。

maven项目有以下几点: project structure

服务类工作正常,包括建议部分(我验证了使用带有main方法的简单测试类)。

问题是我无法让其他控制器工作(我也尝试过基于SOAP的服务,但我在自动装配方面遇到了问题)。

应用程序正在Tomcat上部署,但我找不到任何公开的资源(我正在使用Rest Shell进行测试)。

以下是该项目的一些其他文件: 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>BRISService</groupId>
  <artifactId>BRISService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>BRIS REST Service</name>
  <packaging>war</packaging>
   <dependencies>  	
  	<!-- Spring Framework -->
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>5.0.0.M5</version>
	</dependency> 
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-expression</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>	
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-core</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-beans</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-webmvc</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-web</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>	
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-aop</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-tx</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-orm</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>5.0.0.M5</version>
	</dependency>						
	
	<!-- Datasource -->	
	<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
	<dependency>
    	<groupId>org.apache.commons</groupId>
    	<artifactId>commons-dbcp2</artifactId>
    	<version>2.1</version>
	</dependency>		
	
	<!-- JPA -->
	<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
	<dependency>
    	<groupId>org.hibernate.javax.persistence</groupId>
    	<artifactId>hibernate-jpa-2.1-api</artifactId>
    	<version>1.0.0.Final</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
	<dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-core</artifactId>
    	<version>5.2.8.Final</version>
	</dependency>
	
	<!-- <dependency>
 		<groupId>com.oracle.jdbc</groupId>
 		<artifactId>ojdbc7</artifactId>
 		<version>12.1.0.2</version>
	</dependency> -->
	
	<!-- log4j -->
	<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
	<dependency>
    	<groupId>org.apache.logging.log4j</groupId>
    	<artifactId>log4j-core</artifactId>
    	<version>2.8.1</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
	<dependency>
	    <groupId>commons-logging</groupId>
	    <artifactId>commons-logging</artifactId>
	    <version>1.2.0.redhat-2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-pool2</artifactId>
	    <version>2.4.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
	<dependency>
    	<groupId>org.aspectj</groupId>
    	<artifactId>aspectjrt</artifactId>
    	<version>1.8.10</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
	<dependency>
	    <groupId>org.aspectj</groupId>
    	<artifactId>aspectjtools</artifactId>
    	<version>1.8.10</version>
	</dependency>				
  </dependencies>
  <build>
    <testSourceDirectory>src/main/test</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/webapp/META-INF</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/webapp/WEB-INF</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>BRISService</display-name>
   
   <context-param>
  	<param-name>log4jConfigLocation</param-name>
  	<param-value>classpath:log4j.xml</param-value>
  </context-param>
  
  <listener>
  	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
   
   <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/Dispatcher-servlet.xml</param-value>
    	</init-param>
       <load-on-startup>1</load-on-startup>       
    </servlet>
  
    <servlet-mapping>
       <servlet-name>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/application.xml</param-value>
   </context-param>
</web-app>

分派器-servlet.xml中:

<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"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:webflow="http://www.springframework.org/schema/webflow-config"
	xmlns:tx="http://www.springframework.org/schema/tx" 

	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
        http://www.springframework.org/schema/webflow-config
        http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd        
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- we are using autowiring and component scanning for our beans you can 
		wire them manually if you prefer -->
	<context:component-scan base-package="ro.onrc.bris"/>
	
	<!-- This will automatically switch on the default httpmessageconverters -->
	<mvc:annotation-driven/>	

</beans>

application.xml中:

<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"
	   xmlns:mvc="http://www.springframework.org/schema/mvc" 
	   xmlns:webflow="http://www.springframework.org/schema/webflow-config"
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xmlns:aop="http://www.springframework.org/schema/aop"	    

	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
        http://www.springframework.org/schema/webflow-config
        http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd        
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- we are using autowiring and component scanning for our beans you can 
		wire them manually if you prefer -->
	<context:component-scan base-package="ro.onrc.bris">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- Datasources -->
	<bean id="datasource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
        <property name="URL" value="jdbc:oracle:thin:@172..../..."/>
        <property name="user" value="..."/>
        <property name="password" value="..."/>
        <property name="connectionCachingEnabled" value="true"/>
    </bean>	
	
	<!--  and automatic transactions -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
 		 <property name="entityManagerFactory" ref="entityManagerFactory"/> 
 	</bean>
 	<tx:annotation-driven /> 	

	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    	<property name="dataSource" ref="datasource"/>
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="true"/>
				<property name="generateDdl" value="false"/>
			</bean>
		</property>		
    </bean>
    
    <!-- AOP -->
    <aop:aspectj-autoproxy />
    
    <!-- Advice -->
    <bean id="serviceAdvice" class="ro.onrc.bris.advice.ServiceAdvice"/>
 
</beans>

我的服务类:

package ro.onrc.bris.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ro.onrc.bris.dao.CompanyDao;
import ro.onrc.bris.exception.NoResultsFoundException;
import ro.onrc.bris.model.Company;
import ro.onrc.bris.service.CompanyService;

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	CompanyDao companydao;		
	
	@Override
	public Company searchByID(UUID id) throws NoResultsFoundException 
	{
		Company company = companydao.searchByID(id);
		if (company == null)
			throw new NoResultsFoundException();
		
		return company;
	}

	@Override
	public List<Company> searchByFiscalCode(String fiscalcode) throws NoResultsFoundException 
	{
		List<Company> companies = companydao.searchByFiscalCode(fiscalcode);
		if (companies == null)
			throw new NoResultsFoundException();
		
		return companies;
	}

	@Override
	public Company searchByRegistrationNumber(String registrationnumber) throws NoResultsFoundException 
	{
		Company company = companydao.searchByRegistrationNumber(registrationnumber);
		if (company == null)
			throw new NoResultsFoundException();
		
		return company;
	}

}

我的休息控制器类:

package ro.onrc.bris.ws;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ro.onrc.bris.exception.NoResultsFoundException;
import ro.onrc.bris.model.Company;
import ro.onrc.bris.service.CompanyService;

@RestController("/bris")
public class CompanyRESTService
{
	@Autowired
	private CompanyService companyservice;	
	
	@RequestMapping(value="company/{fiscalcode}")
	public List<Company> searchByFiscalCode(@PathVariable String fiscalcode) throws NoResultsFoundException {
		return companyservice.searchByFiscalCode(fiscalcode);
	}	
}

请帮我配置这个项目。

0 个答案:

没有答案