Wenn man sich mit einem neuem Thema beschäftigt ist es immer gut, wenn man ein paar Anlaufstellen dafür hat. Zum Thema REST Webservices mit Java sind die youtube Playliststs von Java Brains ein guter Anfang:
Für den JAX-RS Standard (aktuell in der Version 2.1) gibt es drei Implementierungen:
Für welche der Implementierungen man sich entscheidet, ist Geschmackssache. Jersey ist die Referenzimplementierung, Apache CXF hat die sehr liberale Apache Lizenz und eine große Community und RESTEasy spielt seine Vorteile aus, wenn man den Webservice auf dem JBoss Applikationsserver deployen will.
Ich habe mich für Apache CXF entschieden. Der Source Code und die Code Beispiele befinden sich auf github. Das Dependency- und Buildmanagement wird über Maven realisiert. Da ich Gradle lieber mag, gibt es hier eine Beispielkonfiguration:
plugins { id 'java' id 'application' id 'eclipse' id 'war' id 'org.gretty' version '2.2.0' } repositories { mavenCentral() jcenter { url "http://jcenter.bintray.com/" } } version "1.0" war.baseName = 'apache-cxf-example' dependencies { // https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty compile group: 'org.apache.cxf', name: 'cxf-rt-transports-http-jetty', version: '3.2.6' // https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http compile group: 'org.apache.cxf', name: 'cxf-rt-transports-http', version: '3.2.6' // https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxrs compile group: 'org.apache.cxf', name: 'cxf-rt-frontend-jaxrs', version: '3.2.6' // https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.1.1' compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1' // https://mvnrepository.com/artifact/org.springframework/spring-core compile group: 'org.springframework', name: 'spring-core', version: '5.1.0.RELEASE' // https://mvnrepository.com/artifact/org.springframework/spring-aop compile group: 'org.springframework', name: 'spring-aop', version: '5.1.0.RELEASE' // https://mvnrepository.com/artifact/org.springframework/spring-context compile group: 'org.springframework', name: 'spring-context', version: '5.1.0.RELEASE' // https://mvnrepository.com/artifact/org.springframework/spring-web compile group: 'org.springframework', name: 'spring-web', version: '5.1.0.RELEASE' // https://mvnrepository.com/artifact/org.springframework.security/spring-security-config compile group: 'org.springframework.security', name: 'spring-security-config', version: '5.1.0.RELEASE' // https://mvnrepository.com/artifact/org.springframework.security/spring-security-web compile group: 'org.springframework.security', name: 'spring-security-web', version: '5.1.0.RELEASE' // https://mvnrepository.com/artifact/org.aspectj/aspectjweaver compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.1' // https://mvnrepository.com/artifact/org.slf4j/slf4j-api compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' // https://mvnrepository.com/artifact/ch.qos.logback/logback-classic runtime group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.13' // runtime group: 'log4j', name: 'log4j', version: '1.2.17' runtime name: 'ojdbc6' // to find the dbcp2 Classes gretty 'org.apache.tomcat:tomcat-dbcp:8.0.51' } gretty { servletContainer='tomcat8' httpPort = 8080 contextPath = '/apache-cxf-example' enableNaming = true }
Die Projektstruktur sieht dann wie folgt aus:
src ├───main │ ├───java │ ├───resources │ └───webapp │ ├───META-INF │ │ └───cxf │ └───WEB-INF │ └───schemas └───test └───java
Verwendet man XML als Austauschformat zwischen Client und Server macht JAX-RS ausgiebig Gebrauch von JAXB. Eine Kurzanleitung gibt es hier.
Für die Authentifizierung und Authorisierung eignet sich sehr gut Spring-Security. Ein Beispiel dazu befindet sich im Code auf github.