简体   繁体   English

如何在Spring 5中公开服务?

[英]How to expose service with spring 5?

I want to expose service with Spring framework (not with spring boot). 我想使用Spring框架(而不是Spring Boot)公开服务。 Then i can use the service to feed a dashboard. 然后,我可以使用该服务来填充仪表板。 Charts in the dashboard need data with json format. 仪表板中的图表需要json格式的数据。 My question is similar to this topic but with more question about code.[question]: Expose Service Layer directly in spring mvc 我的问题与该主题相似,但有更多关于代码的问题。[问题]: 直接在spring mvc中公开服务层

I first did the model, repository to access database. 我首先做了模型,访问数据库的仓库。 I am using Hibernate and MySQL. 我正在使用Hibernate和MySQL。 I run my application with a class containing the main method. 我使用包含main方法的类运行我的应用程序。 Then i tried to add a rest controller to access the method findAll. 然后,我尝试添加一个rest控制器来访问方法findAll。 But when i deployed the application on Tomcat, i only get the message 404 not found. 但是,当我在Tomcat上部署应用程序时,我只会收到未找到消息404。

This is my first 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;
    }
}

this is the interface 这是界面

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

this is the implementation of findAll method 这是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;
}

EDIT: web .xml 编辑:web .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 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 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> 

Should i add servlet , dispacher servlet , application context to find the resource through URI ? 我是否应该添加servlet,分发servlet,应用程序上下文以通过URI查找资源?

I don't know what is exactly the url you are using to test the service but if you are trying to invoke /fruit/all, it won't work because the servlet dispatcher is configured to handle request that ends with .form. 我不知道您用来测试服务的URL到底是什么,但是如果您尝试调用/ fruit / all,则将无法正常工作,因为Servlet调度程序已配置为处理以.form结尾的请求。 To make it work you should change the url-pattern of the servlet dispatcher to something like /fruit/* 为了使其工作,您应该将servlet调度程序的url-pattern更改为/ fruit / *之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 kubernetes-如何将 mysql 服务暴露给另一个 pod? - kubernetes-How do I expose mysql service to another pod? 如何在Talend中将mysql数据公开为soap Web服务 - How to expose mysql data as a soap web service in Talend 如何将 MySQL 数据库公开为 OData - How to expose a MySQL database as OData 如何在 Spring Boot 中使用来自 UI 服务的登录服务? - How to consume Login Service from UI Service in Spring Boot? Node-JS公开Mysql DB作为Rest Service - Node-JS Expose Mysql DB as Rest Service 我如何在Spring,MySQL中在一个服务中运行插入脚本 - How do I run insert scripts in one service in spring, mysql Spring @Repository和@Service - Spring @Repository and @Service 如何在基于带有mysql数据库的tomcat服务器的AWS上部署Java Spring Restful Service? - How to deploy java spring restful service on AWS based on tomcat server with mysql database? 如何在具有MySQL后端的JPA Spring Boot微服务中为三向关系建模 - How to model a three-way relationship in a JPA Spring Boot micro service with a MySQL back end Spring 引导服务未连接到 GKE 的 kubernetes 内的 mysql DB 服务 - Spring boot service is not connecting to mysql DB service inside kubernetes of GKE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM