mysql - 如何使用 Spring 5 公开服务?

标签 mysql spring hibernate jpa service

我想使用 Spring 框架(而不是 Spring Boot)公开服务。然后我可以使用该服务来提供仪表板。仪表板中的图表需要 json 格式的数据。我的问题与此主题类似,但有更多关于代码的问题。[问题]:Expose Service Layer directly in spring mvc

我首先做了模型、存储库来访问数据库。我正在使用 Hibernate 和 MySQL。我使用包含 main 方法的类运行我的应用程序。然后我尝试添加一个休息 Controller 来访问方法 findAll。但是当我在 Tomcat 上部署应用程序时,我只收到消息 404 not found。

这是我的第一个 Controller

@RestController
@RequestMapping("/fruit")
public class FruitController {

    @Autowired
    private IFruitRepository fruitRepo = new FruitRepository();


    @RequestMapping( value = "/all", method = RequestMethod.GET )
    public @ResponseBody List<Port> getFruit() {
        List<Fruit> res = fruitRepo.findAll();
        return res;
    }
}

这是界面

public interface IFruitRepository {
    Boolean create(Fruit p);
    Fruit findById(int id);
    List<Fruit> findAll();
    Fruit update(Fruit f);
    boolean delete(int id);
}

这是findAll方法的实现

public List<Fruit> findAll(){
    List<Fruit> à_retourner = new ArrayList<>();

    try (SessionFactory factory = HibernateUtil.getSessionFactory()) {
        Session session = factory.openSession();
        Query query = session.createQuery("from Fruit");
        à_retourner = query.getResultList();
    } catch (Exception e) {
        System.out.println("exception _ findAll _ Fruit : " + e);
    }
    return à_retourner;
}

编辑: 网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>*.form</url-pattern>
    </servlet-mapping>
</web-app>

dispacher-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

applicationcontext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

我应该添加 servlet 、调度程序 servlet 、应用程序上下文来通过 URI 查找资源吗?

最佳答案

我不知道您用来测试服务的 url 到底是什么,但如果您尝试调用/fruit/all,它将无法工作,因为 servlet 调度程序被配置为处理以 .形式。为了使其工作,您应该将 servlet 调度程序的 url-pattern 更改为/fruit/* 之类的内容

关于mysql - 如何使用 Spring 5 公开服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944813/

相关文章:

spring - 如何在 Thymeleaf 和 Spring Boot 中显示消息?

java - hibernate :在选择调用时执行更新

mysql - 通过另一个外键关联外键

java - Spring - 添加一个低优先级的多线程服务(不影响生产性能)

Mysql语法错误: deleting records

spring - Grails 不使用 message.properties 来显示错误消息?

java - 如何在 hql 查询中传递列表,其中列表是 map 的值

hibernate - 为什么 Spring HibernateTemplate loadAll() 方法会为每一行生成更新?

c++ - C/C++ 中 MySQL 的静态链接

php - mysql 连接具有相同列名的多个表 |Codeigniter|