【问题标题】:How do I configure my spring web app to run on my local server?如何配置我的 Spring Web 应用程序以在本地服务器上运行?
【发布时间】:2021-12-17 12:10:16
【问题描述】:

所以我重新开始开发,但我无法让我的应用程序运行并返回我的空数组,以便我知道我的应用程序正在运行并连接到我的 RDS。

我查看了许多问题和资源,但没有找到解决问题的方法。

我能够运行我的应用程序,但收到此错误:

错误

HTTP Status 404 – Not Found

Type Status Report

Message The requested resource [/PartyRoom/users/all] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.54

这是我的项目

型号

@Entity
@Table(name="Users")
public class Users implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name="username", unique = true)
private String username;

@Column(name="first_name")
private String fName;

@Column(name="last_name")
private String lName;

@Column(name="password")
private String password;

@Pattern(regexp="\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,6}")
@Column(name="email")
private String email;

public Users() {
    super();
}

public Users(String fName, String lName, String username, String password, String email) {
    this.fName = fName;
    this.lName = lName;
    this.username = username;
    this.password = password;
    this.email = email;
}

public Users(String username, String password) {
    this.username = username;
    this.password = password;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getfName() {
    return fName;
}

public void setfName(String fName) {
    this.fName = fName;
}

public String getlName() {
    return lName;
}

public void setlName(String lName) {
    this.lName = lName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Override
public String toString() {
    return "User [username=" + username + ", fName=" + fName + ", lName=" + lName + ", password=" + password
            + ", email=" + email + "]";
 }  

}

存储库

@Transactional
@EnableTransactionManagement
@Repository
public class UserRepo {

@Autowired
SessionFactory sf;

public List<Users> getUsers(){
    List<Users> users = new ArrayList<>();
    Session s = sf.getCurrentSession();
    Criteria cr = s.createCriteria(Users.class);
    users = cr.list();
    return users;
}

public void createNewUser(Users user) {
    Session s = sf.getCurrentSession();
    s.save(user);
 }
}

服务

@Service
public class UserService {

@Autowired
UserRepo ur;

public List<Users> getAllUsers()
{
    return ur.getUsers();
}

public void addUser(Users user)
{
    ur.createNewUser(user);
    
}

}

控制器

@RestController
@RequestMapping("/users")
@CrossOrigin(origins= "*", allowedHeaders = "*", value = "*")
public class UserController {

@Autowired
UserService us;

@GetMapping("/all")
public ResponseEntity<List<Users>> getAllUsers() {
    return new ResponseEntity<>(us.getAllUsers(), HttpStatus.OK);
}

@PostMapping("/add")
public void insertAdministrator(@RequestBody Users user) {
    us.addUser(user);
    
}

}

休眠配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate- 
configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">
         com.mysql.jdbc.Driver
    </property>
    
    <property name = "hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    
    <property name="hibernate.connection.url">
        jdbc:mysql://xxxxxxx.rds.amazonaws.com/3306/PartyRoom
    </property>
    
    <property name="hibernate.connection.username">
        username
    </property>
    
    <property name="hibernate.connection.password">
        password
    </property>
    
    <!-- Determines how many connections a single user is allowed to a database -->
    <property name="hibernate.connection.pool_size">
        2
    </property>
    
    <property name="show_sql">
        true
    </property>
    
    <!-- This is what tells hibernate how to deal with the database -->
    <property name="hibernate.hbm2ddl.auto">
        update
    </property>
    
</session-factory>

应用程序上下文

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
    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-4.3.xsd">

<!-- Scan for Spring beans -->

<context:component-scan base-package="repositories, models, services, controllers"></context:component-scan>

<!-- Use Annotations for MVC -->
<mvc:annotation-driven />

<!-- Hibernate config Goes here -->
<!-- configure dataSource bean -->
<bean name="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"
        value="com.mysql.jdbc.Driver" />
    <property name="url"
        value="jdbc:mysql://xxxxxxxx.rds.amazonaws.com/3306/PartyRoom" />
    <property name="username" value="username" />
    <property name="password" value="password" />
</bean>

<!-- configure session factory bean -->

<bean name="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="models" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <!-- <prop key="hibernate.show_sql">true</prop> -->
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
<!--Transaction Manager (Hibernate) makes all session transactions -->

<bean name="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


 </beans>

调度程序上下文

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
    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-4.3.xsd">

<!-- Scan for Spring beans -->
<context:component-scan base-package="repositories, model, services, controllers"></context:component-scan>


<!-- Use Annotations for MVC -->
<mvc:annotation-driven />

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_4_0.xsd" 
     version="4.0">
     
  <display-name>Party-Room</display-name>

<context-param>
    <param-name>ContextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>
    <servlet-name>FCServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>ContextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcherContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
    <servlet-name>FCServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

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

我正在寻找的结果

当我转到端点 PartRoom/user/all 时,我正在寻找要返回的空数组。

【问题讨论】:

  • 为什么要使用遗留的配置系统,而不是使用Spring Boot,这样容易多了?
  • 这是我第一次学到的东西,所以我用这个来刷新我的记忆,之后将转向 Spring Boot。

标签: java spring model-view-controller web-applications


【解决方案1】:

PartRoom 没有映射到任何地方。您的控制器映射到/user。所以请尝试/users/all

【讨论】:

  • PartyRoom 是我的项目名称,/users/all 会也不会正常工作。
猜你喜欢
  • 1970-01-01
  • 2020-10-22
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 2019-08-06
  • 1970-01-01
  • 2011-06-17
相关资源
最近更新 更多