简体   繁体   中英

How to expose service with spring 5?

I want to expose service with Spring framework (not with spring boot). Then i can use the service to feed a dashboard. Charts in the dashboard need data with json format. My question is similar to this topic but with more question about code.[question]: Expose Service Layer directly in spring mvc

I first did the model, repository to access database. I am using Hibernate and MySQL. I run my application with a class containing the main method. Then i tried to add a rest controller to access the method findAll. But when i deployed the application on Tomcat, i only get the message 404 not found.

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

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

 <?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> 

Should i add servlet , dispacher servlet , application context to find the resource through 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. To make it work you should change the url-pattern of the servlet dispatcher to something like /fruit/*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM