Um zu lernen (Spring Boot ist großartig und magisch ..), versuche ich eine Web-App mit Jersey einrichten und Frühling (für DI). Ich habe es geschafft, aber es funktioniert nur mit Spring Xml-Konfiguration.Wie man Jersey 2/Spring 4 lädt Beans von @Configuration auf einer no web.xml Konfiguration
I erweitert ResourceConfig
@ApplicationPath("/api")
public class JerseyWebApp extends ResourceConfig {
public JerseyWebApp() {
packages("com.company.controller");
}
}
Mein HelloController.java
@Path("/hello")
public class HelloController {
private HelloService helloService;
@Autowired
public HelloController(HelloService helloService) {
this.helloService = helloService;
}
@GET
public String hello() {
return helloService.sayHello();
}
}
mein HelloService.java
@Component
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public class HelloService {
public String sayHello() {
return "hello from jersey webapp";
}
}
und meine Feder applicationContext.xml
Anbringen Auch meine pom-Datei
<properties>
<jersey2.version>2.23.2</jersey2.version>
<jaxrs.version>2.0.1</jaxrs.version>
<spring.version>4.3.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey2.version}</version>
<exclusions>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-web</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>jersey-server</artifactId>
<groupId>org.glassfish.jersey.core</groupId>
</exclusion>
<exclusion>
<artifactId>
jersey-container-servlet-core
</artifactId>
<groupId>org.glassfish.jersey.containers</groupId>
</exclusion>
<exclusion>
<artifactId>hk2</artifactId>
<groupId>org.glassfish.hk2</groupId>
</exclusion>
</exclusions>
</dependency>
<!--Spring 4-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
Ist es möglich, Jersey zu bekommen Bohnen über eine @Configuration Klasse laden oder soll ich prüfen, mit Spring Web/mvc die gesamte Anwendung Bootstrap.
Sehr schöne Arbeit um: Hier ist meins: https://github.com/bric3/jersey2-spring4-example – Brice