gpt4 book ai didi

mysql - Spring 框架 RESTful 服务

转载 作者:行者123 更新时间:2023-11-29 06:05:20 25 4
gpt4 key购买 nike

我正在 Eclipse 上构建我的第一个 Spring RESTful API。我无法调用 API。我得到“请求的资源不可用”。使用 http://localhost:8080/InterVUEService/api/Clinic/7 时出错.

WEB-INF/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>InterVUEService</display-name>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- SERVLET DEFINITIONS -->
<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/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/InterVUEService/*</url-pattern>
</servlet-mapping>


<session-config>
<session-timeout>30</session-timeout>
</session-config>

</web-app>

ApplicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
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/task http://www.springframework.org/schema/task/spring-task.xsd">

<!--Enable scanning of spring @Configuration classes -->
<context:annotation-config />

<!-- import core modules beans -->
<import resource="classpath:spring/application-context-core.xml" />
</beans>

application-context-core.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:task="http://www.springframework.org/schema/task"
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">

<!-- scan base package for components -->
<context:component-scan
base-package="com.abaxis.interVUEservice.*dao" />

<import resource="datasource-beans.xml" />



<!-- <task:annotation-driven /> -->

</beans>

datasource-beans.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:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

<jee:jndi-lookup
id="dataSource"
jndi-name="jdbc/vetscanVueDS"
expected-type="javax.sql.DataSource"/>

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" >
<qualifier value="mysqlDB"/>
</bean>

<tx:advice id="txMysqlAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mybatis/maps/*.xml"/>
<property name="typeAliasesPackage" value="com.abaxis.interVUEservice.model"/>
<property name="configLocation" value="classpath:mybatis/sqlmap-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.abaxis.interVUEservice.*dao"/>
</bean>
</beans>

com.xxx.interVUEservice.api.implClinicResourceImpl.java

enter code here
package com.xxx.interVUEservice.api.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xxx.interVUEservice.api.ClinicResource;
import com.xxx.interVUEservice.common.exception.BusinessException;
import com.xxx.interVUEservice.model.Clinic;
import com.xxx.interVUEservice.service.ClinicService;

@Controller
@RequestMapping(value = "/api/Clinic/")
public class ClinicResourceImpl implements ClinicResource {


@Autowired
private ClinicService clinicService;

@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public List<Clinic> ClinicList() throws BusinessException {
// TODO Auto-generated method stub
return null;
}

@RequestMapping(value = "/{clinicId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Clinic ClinicInfo(@PathVariable("clinicId") int id) {

Clinic temp = new Clinic();

temp.setId(id);
temp.setName("ABC");
temp.setLicenseNumber("999999");
temp.setAddress("3240 XXX");
temp.setCity("XX");
temp.setState("XX");
temp.setPostcode("94599");
temp.setCountry("XXX");
temp.setPhoneNumber("0000-0000-0000");
temp.setActive(true);
temp.setLanguage("en");

return temp;
//Clinic clinic = clinicService.getClinic(id);

//return clinic;
}

pom.xml

enter code here
<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.abaxis</groupId>
<artifactId>InterVUEService</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>InterVUEService Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<spring.version>4.3.2.RELEASE</spring.version>
<mybatis.version>3.2.3</mybatis.version>
<mybatis-spring.version>1.2.1</mybatis-spring.version>
<mysql.version>5.1.5</mysql.version>
<tomcat.version>7.0.72</tomcat.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Check if the following dependencies are required-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>

</dependencies>
<build>
<finalName>InterVUEService</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<verbose>true</verbose>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<contextFile>${basedir}/docs/context.xml
</contextFile>
</configuration>


<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

你能告诉我为什么我不能调用http://localhost:8080/api/Clinic/id吗? ?为什么这个 Uri 不能在我的 Tomcat 服务器上运行?请帮忙。我是一个新手,正在学习为我的下一个工作项目编写 Restful 服务以在 AWS 上托管。

谢谢纳利尼

最佳答案

<context:component-scan base-package="com.abaxis.interVUEservice.*dao"/>

你能试试改成

<context:component-scan  base-package="com.abaxis.interVUEservice" />

并使用http://localhost:8080/InterVUEService/api/Clinic/7

关于mysql - Spring 框架 RESTful 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41968518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com