I created a program in cucumber and facing issue like this.
pom.xml, step defination file , feature file and ruunable file are created.
In pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CucumberTest1</groupId>
<artifactId>CucumberTest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
**Annotation.java*
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
public class annotation {
WebDriver driver = null;
#Given("^I am on Facebook login page$")
public void goToFacebook()
{ System.setProperty("webdriver.firefox.marionette","C:\\Users\\Administrator\\Downloads\\geckodriver-v0.15.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
}
#When("^I enter username as \"(.*)\"$")
public void enterUsername(String arg1) {
driver.findElement(By.id("email")).sendKeys(arg1);
}
#When ("^I enter password as \"(.*)\"$")
public void enterPassword(String arg1) {
driver.findElement(By.id("pass")).sendKeys(arg1);
driver.findElement(By.id("u_0_v")).click();
}
#Then("^Login should fail$")
public void checkFail() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test1 Pass");
} else {
System.out.println("Test1 Failed");
}
driver.close();
}
#Then("^Relogin option should be available$")
public void checkRelogin() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test2 Pass");
} else {
System.out.println("Test2 Failed");
}
driver.close();
}
}
Ruunable class
package Annotation;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"pretty", "html:target/cucumber"})
public class runTest { }
featurefile
Feature: annotation
#This is how background can be used to eliminate duplicate steps
Background:
User navigates to Facebook Given
I am on Facebook login page
#Scenario with AND
Scenario:
When I enter username as "TOM"
And I enter password as "JERRY"
Then Login should fail
#Scenario with BUT
Scenario:
When I enter username as "TOM"
And I enter password as "JERRY"
Then Login should fail
But Relogin option should be available
OUTPUT ON CONSOLE
Feature: annotation
#This is how background can be used to eliminate duplicate steps
Background: [90m# Annotation\outline.feature:5[0m
User navigates to Facebook Given
I am on Facebook login page
#Scenario with AND
Scenario: [90m# Annotation\outline.feature:10[0m
[90mWhen [0m[90mI enter username as "[0m[90m[1mTOM[0m[90m"[0m [90m# annotation.enterUsername(String)[0m
[1A [31mWhen [0m[31mI enter username as "[0m[31m[1mTOM[0m[31m"[0m [90m# annotation.enterUsername(String)[0m
[31mjava.lang.NullPointerException
at Annotation.annotation.enterUsername(annotation.java:23)
at ?.When I enter username as "TOM"(Annotation\outline.feature:11)
[0m
[90mAnd [0m[90mI enter password as "[0m[90m[1mJERRY[0m[90m"[0m [90m# annotation.enterPassword(String)[0m
[1A [36mAnd [0m[36mI enter password as "[0m[36m[1mJERRY[0m[36m"[0m [90m# annotation.enterPassword(String)[0m
[90mThen [0m[90mLogin should fail[0m [90m# annotation.checkFail()[0m
[1A [36mThen [0m[36mLogin should fail[0m [90m# annotation.checkFail()[0m
#This is how background can be used to eliminate duplicate steps
Background: [90m# Annotation\outline.feature:5[0m
User navigates to Facebook Given
I am on Facebook login page
#Scenario with BUT
Scenario: [90m# Annotation\outline.feature:16[0m
[90mWhen [0m[90mI enter username as "[0m[90m[1mTOM[0m[90m"[0m [90m# annotation.enterUsername(String)[0m
[1A [31mWhen [0m[31mI enter username as "[0m[31m[1mTOM[0m[31m"[0m [90m# annotation.enterUsername(String)[0m
[31mjava.lang.NullPointerException
at Annotation.annotation.enterUsername(annotation.java:23)
at ?.When I enter username as "TOM"(Annotation\outline.feature:17)
[0m
[90mAnd [0m[90mI enter password as "[0m[90m[1mJERRY[0m[90m"[0m [90m# annotation.enterPassword(String)[0m
[1A [36mAnd [0m[36mI enter password as "[0m[36m[1mJERRY[0m[36m"[0m [90m# annotation.enterPassword(String)[0m
[90mThen [0m[90mLogin should fail[0m [90m# an notation.checkFail()[0m
[1A [36mThen [0m[36mLogin should fail[0m [90m# annotation.checkFail()[0m
[90mBut [0m[90mRelogin option should be available[0m [90m# annotation.checkRelogin()[0m
[1A [36mBut [0m[36mRelogin option should be available[0m [90m# annotation.checkRelogin()[0m
java.lang.NullPointerException
at Annotation.annotation.enterUsername(annotation.java:23)
at ?.When I enter username as "TOM"(Annotation\outline.feature:11)
java.lang.NullPointerException
at Annotation.annotation.enterUsername(annotation.java:23)
at ?.When I enter username as "TOM"(Annotation\outline.feature:17
Please change background part of your feature file as below
Background: User navigates to Facebook
Given I am on Facebook login page
Currently it is like,
Background:
User navigates to Facebook Given
I am on Facebook login page
Please update me whether its solved issue or not?
Try this:
System.setProperty("webdriver.gecko.driver","C:\Users\Administrator\Downloads\geckodriver-v0.15.0-win64\geckodriver.exe");
Hope this solves the issue!
Related
AccessTokenVerifier
How to write juint test case for AccessTokenverifier. I have attached the screenshot , code and dependencies details. please help if anyone know this.
please review the screershot.
Jwt token verifier source code screenshot
Source Code
log.info("JwtVerifier start building to verify the authToken");
AccessTokenVerifier jwtVerifier =
JwtVerifiers.accessTokenVerifierBuilder()
.setIssuer(oktaTokenUrlIssuer)
.setAudience(TOKEN_AUDIENCE)
.setConnectionTimeout(Duration.ofSeconds(1))
.build();
jwtVerifier.decode(authToken);
log.info("JwtVerifier build and verified the authToken successfully");
Reference Link : okta verifier
Dependencies
<dependency>
<groupId>com.okta.jwt</groupId>
<artifactId>okta-jwt-verifier</artifactId>
<version>${okta-jwt.version}</version>
</dependency>
<dependency>
<groupId>com.okta.jwt</groupId>
<artifactId>okta-jwt-verifier-impl</artifactId>
<version>${okta-jwt.version}</version>
<scope>runtime</scope>
</dependency>
I got exception when try to write test case com.okta.jwt.JwtVerificationException: Failed to parse token
Below I have attached my test case.
please refer this screenshot : My test case .
my goal is to write test case to cover 100%.
Something like this:
import org.junit.Test;
import static org.junit.Assert.*;
public class JwtVerifierTest {
#Test
public void testVerifierBuildAndVerification() {
String authToken = "valid_auth_token";
String oktaTokenUrlIssuer = "https://my-okta-instance.com";
String TOKEN_AUDIENCE = "api://default";
AccessTokenVerifier jwtVerifier =
JwtVerifiers.accessTokenVerifierBuilder()
.setIssuer(oktaTokenUrlIssuer)
.setAudience(TOKEN_AUDIENCE)
.setConnectionTimeout(Duration.ofSeconds(1))
.build();
try {
jwtVerifier.decode(authToken);
assertTrue(true);
} catch (Exception e) {
fail("Verification failed");
}
}
}
I'm new to the UnitTesting and Cucumber, and today I tried to implement a simple example from a tutorial in Eclipse and I got error when I try run the RunnerClass.java.
My Pom file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>belajar1</groupId>
<artifactId>belajar1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>9</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.0-beta4</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.10.2</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.10.2</version>
</dependency>
</dependencies>
</project>
My feature file
Feature: Gmail Testing
Scenario: Gmail Login
Given url opened
Then enter user id and click next
Then enter password
And click login
Scenario: Gmail Close
Then Close browser
My Definition file
public class stepDefinition {
public static WebDriver obj=null;
#Given("^url opened$")
public void url_opened() throws Throwable{
System.setProperty("webdriver.gecko.driver","D:\\Installed APP\\Eclipse Workspace\\Webdriver\\geckodriver.exe");
obj=new FirefoxDriver();
obj.manage().window().maximize();
obj.get("https://mail.google.com");
}
#Then("enter user id and click next")
public void enter_user_id_and_click_next() throws InterruptedException{
obj.findElement(By.id("Email")).sendKeys("YOURUSERID");
obj.findElement(By.id("next")).click();
Thread.sleep(2000);
}
#Then("^enter password$")
public void enter_password(){
obj.findElement(By.id("Passwd")).sendKeys("YOURPASSWORD");
}
#Then("^click login$")
public void click_login() throws InterruptedException{
obj.findElement(By.id("signIn")).click();
Thread.sleep(6000);
}
#Then("^close browser$")
public void close_browser(){
obj.quit();
}
}
My Runner file
package tester;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features="src/test/resources/demo.feature",
glue="definition.stepDefinition",
plugin = {
"pretty",
"html:target/report", //create a folder called cucumber
"json:target/report.json",
//Notice the corresponding file extension (.json) telling cucumber to create a file
// "com.cucumber.listener.ExtentCucumberFormatter:target/report.html"
//Notice I remove the white space at :target/report.html
}
)
public class RunnerClass {
}
I keep got this error
enter image description here
Also this my Library
enter image description here
I'm trying to create a simple Junit5-Cucumber project (in Eclipse) that would be used for UI testing.
I took reference from this repo:https://github.com/cucumber/cucumber-java-skeleton
Issue: No definition found for Open the Chrome and launch the application (error happens to the Given, When and Then statements) in the test_features.feature file.
# test_features.feature
Feature: Reset functionality on login page of Application
Scenario: Verification of Reset button
Given Open the Chrome and launch the application
When Enter the username and password
Then Reset the credentials
# RunCucumberTest.java
package lpms.cucumber;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;
import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
#Suite
#IncludeEngines("cucumber")
#SelectClasspathResource("lpms/cucumber")
#ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
#ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "lpms.cucumber")
public class RunCucumberTest {
}
# StepDefinitions.java
package lpms.cucumber;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class StepDefinitions {
#Given("^Open the Chrome and launch the application$")
public void open_the_chrome_and_launch_the_application() throws Throwable
{
System.out.println("This step opens the chrome and launches the application");
}
#When("^Enter the username and password$")
public void enter_the_username_and_password() throws Throwable
{
System.out.println("This step enters the username and password on the login page");
}
#Then("^Reset the credentials$")
public void reset_the_credential() throws Throwable
{
System.out.println("This step clicks on the reset button.");
}
}
Project Structure
IMAGE OF MY PROJECT STRUCTURE
Solved!
It's a warning from Eclipse IDE, likely just a bug, because I can still get testing done.
Sidenote: Extremely useful guide for learning the latest cucumber: https://cucumber.io/docs/guides/10-minute-tutorial/
I had the same problem on my project and i'll post my solution here.
I've used Eclipse + Java 11 + SpringBoot 2.6.4
pom.xml dependencies
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
pom.xml plugin in build section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<properties>
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
After that, i've created a package in src/test/java called
filelife/skynet/cucumber
In this package i've created my steps class and my runner class; Steps class contains only some logging instrauctions, it doesn't verify nothing yet.
Steps class:
#Slf4j
public class SendMessagesOnServiceLimitsSteps {
#Given("A ServiceLimits Module with PosTXRate of {int} seconds")
public void a_service_limits_module_with_pos_tx_rate_of_seconds(Integer posTxRate) {
log.info("ServiceLimits PosTxRate {}", posTxRate);
System.out.println("Given Step");
}
#When("I keyOn the device")
public void i_key_on_the_device() {
System.out.println("Given Step");
}
#When("i wait for {int} seconds")
public void i_wait_for_seconds(Integer int1) {
System.out.println("Given Step");
}
#When("i keyOff the device")
public void i_key_off_the_device() {
System.out.println("Given Step");
}
#Then("PositionData messages should be {int} or {int}")
public void position_data_messages_should_be_or(Integer int1, Integer int2) {
System.out.println("Given Step");
}
#Then("device log print {string}")
public void device_log_print(String string) {
System.out.println("Given Step");
}
}
And my runner tests class:
#Suite
#IncludeEngines("cucumber")
#SelectClasspathResource("filelife/skynet/cucumber")
#ConfigurationParameter(
key = GLUE_PROPERTY_NAME,
value = "filelife.skynet.cucumber"
)
public class SkynetTest{
}
I've also created the same folder path (filelife/skynet/cucumber) in src/test/resources source folder and i've pasted my .feature file.
In the end, i've created 2 files:
cucumber.properties
junit-platform.properties
in same source folder src/test/resources containg, both of them, string:
cucumber.publish.quiet=true
This configuration works with:
mvn tests
and
right click on SkynetTest -> RunAs -> Junit Test
I am working an assignment for a class, and am having trouble getting tables to be created in my MySQL database from entities in java. I am trying to get the tables to be created by typing mvn clean install in the project folder in terminal (which is what was given to me as an example to create them once I had the entities in java). No errors or anything occur, and I get a "build successful" message in terminal, but no new tables are created in MySQL. I have confirmed that my endpoint/username/password are all working by setting up the project using jdbc to manually connect instead of JPA and everything works fine that way. Note: This isn't the actual content of the assignment just the initial setup. I've followed the instructions the professor has given multiple times and it is not working. Thanks for the help!
I created my project using the spring command line interface in terminal:
spring init --dependencies=web test
I then added a webapp directory with a index.html file in the src/main directory of the project. Then the project was imported to IntelliJ as a Maven project
I added the following to my application.properties file which is in src/main resources (and is the resources root of the project). The aws endpoint/schema name are also filled in as usual:
spring.datasource.url=jdbc:mysql://MyAWSEndpoint:3306/SchemaName
spring.datasource.username=MyUsername
spring.datasource.password=MyPassword
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
I then have a class that I created called random which is contained in src/main/java which is my source root for the project.
package com.example.test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class random {
#Id
#GeneratedValue (strategy = GenerationType.IDENTITY)
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Additionally I have a Repository I made for the entity in java contained in the same package as the class above.
package com.example.test;
import org.springframework.data.repository.CrudRepository;
public interface RandomRepository extends CrudRepository<random, Integer> {
}
Here is my pom.xml file as well
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Additionally, I have a an application file in src/main/java:
package com.example.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
You might want to use the below property in the application.properties once:
spring.jpa.hibernate.ddl-auto=create
after the first run you can comment it out again.
To be sure your db user must have the correct privileges to create tables otherwise it won't work :-)
And you need to run the application either by using:
mvn clean package && java -jar target/test-0.0.1-SNAPSHOT.jar
or
mvn clean spring-boot:run
if your application is only build it will not run and not do anything except being compiled and tested.
It might be that your teacher had the setup of the database in the unit tests? then it would have been done...
Good luck
I'm trying to develop a REST service using Apache-CXF, on top of JAX-RS. For starters, I have a method called test that receives a String message and int value. I want the clients to be able to pass these parameters in a POST message body. I can't seem to achieve this.
Before I paste the code here, here are some details:
I'm using CXF without Spring
It's not a web app, so I don't have the WEB-INF folder with the web.xml
I test the service using SoapUI and Postman (Google Chrome application)
With the following code, I get WARNING: javax.ws.rs.BadRequestException: HTTP 400 Bad Request:
DemoService.java
#WebService(targetNamespace = "http://demoservice.com")
#Path("/demoService")
public interface DemoService {
#POST
#Path("/test")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String test (String message, int value);
}
DemoServiceImpl.java
public class DemoServiceImpl implements DemoService {
#Override
public String test(String message, int value) {
return "test message: " + message + " value = : " + value;
}
}
DemoServer.java
public class DemoServer{
public static void main(String[] args) {
JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
DemoService demoService = new DemoServiceImpl();
serverFactory.setServiceBean(demoService);
serverFactory.setAddress("http://localhost:9090");
serverFactory.create();
}
}
My POM.xml (minus the attributes in the root tag, everything's there)
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>demoService</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<cxf.version>3.0.0</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description</artifactId>
<version>3.0.0-milestone1</version>
</dependency>
</dependencies>
</project>
Testing with {"message":"hello there!", "value":"50"} to the URL http://localhost:9090/demoService/test gave a HTTP 400 Bad Reuest.
Then I saw this question on S.O.: How to access parameters in a RESTful POST method and tried this:
added the following nested class in DemoServer.java:
#XmlRootElement
public static class TestRequest {
private String message;
private int value;
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public int getValue() { return value; }
public void setValue(int value) { this.value = value; }
}
I also modified the DemoService interface and the implementation to use this class as a parameter in the test method, although this is still ultimately not what I want to do. (just showing the implementation here, question's already getting long):
#Override
public String test(TestRequest testRequest) {
String message = testRequest.getMessage();
int value = testRequest.getValue();
return "test message: " + message + " value = : " + value;
}
And to fix this error that I got: SEVERE: No message body reader has been found for class DemoService$TestRequest, ContentType: application/json (in Postman I see error 415 - unsupported media type) I added the following dependencies (jettison and another thing) to the POM.xml:
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>2.6.0</version>
</dependency>
I tested the service using the following JSON message, in a HTTP POST request:
{"testRequest":{"message":"hello there!", "value":"50"}}
This works. Though this solution where I use a TestRequest class to encapsulate the parameters works, that's not the solution I'm looking for. I want to be able to pass the two parameters in a JSON message, without having to introduce this TestRequest class (explicitly).
Questions:
Would this be easier to implement using Jersey?
I don't have a web.xml nor a WEB-INF folder, so I can't configure CXF in a cxf.xml file can I? A lot of tutorials online seem ot use a lot of XML configuration, but I don't want to deploy a framework like TomEE or Spring or Glassfish just to do that.
Searching online for solutions, I came across Spring Boot. Would you recommend using that, perhaps? Would that make developing web services like this easier?
Also, how do I get it to return the value in JSON format (or is it not supposed to do that for Strings?)
My friend pointed me to this stack exchange question: JAX-RS Post multiple objects
and also the following documentation: http://cxf.apache.org/docs/jax-rs-and-jax-ws.html
which states:
public class CustomerService {
public void doIt(String a, String b) {...};
}
By default JAX-RS may not be able to handle such methods as it
requires that only a single parameter can be available in a signature
that is not annotated by one of the JAX-RS annotations like
#PathParam. So if a 'String a' parameter can be mapped to a #Path
template variable or one of the query segments then this signature
won't need to be changed :
#Path("/customers/{a}")
public class CustomerService {
public void doIt(#PathParam("a") String a, String b) {...};
}
So, to answer my question, NO, it cannot be done.