This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 6 years ago.
I have 3 maven projects:
project-A
project-B
project-C
The second project (B) requires the dependency for the first project (A).
The third project (C) requires the dependencies for the first and second project (A, B).
I have defined these dependencies in the respective projects pom files:
project-B pom.xml :
<dependency>
<groupId>com.mygroupid</groupId>
<artifactId>project-A</artifactId>
<version>${project.version}</version>
</dependency>
project-C pom.xml :
<dependency>
<groupId>com.mygroupid</groupId>
<artifactId>project-A</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mygroupid</groupId>
<artifactId>project-B</artifactId>
<version>${project.version}</version>
</dependency>
Everything works OK on SpringToolSuite. I have tested the second project (which depends on the first project) and also tested the third project (which depends on both first and second project) and everything works OK from STS.
When I try to execute:
mvn clean install -U -DskipTests=true
it works perfectly well for project-A and project-B, but for project-C I get a:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] MyClassOnProjectC.java:[12,38] package XXX does not exist
[ERROR] MyClassOnProjectC.java:[22,17] cannot find symbol
symbol: class MyClassOnProjectB
location: class MyClassOnProjectC
Below there are 3 classes extracted from each of the projects:
Project-A:
User.java
package com.neweraed.datamodel;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "UserType", propOrder = {
"name",
"description",
})
public class User implements Serializable {
private static final long serialVersionUID = -876063526825526098L;
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Project-B:
UserOperations.java
package com.neweraed.services.midpoint;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;
import com.neweraed.datamodel.User;
import com.neweraed.services.midpoint.utils.Constants;
import com.neweraed.services.midpoint.utils.Utils;
#Component
public class UserOperations {
private static final Logger logger = LoggerFactory.getLogger(UserOperations.class);
public User getUser(User user) {
RestOperations restOperations = Utils.createGenericRestTemplate();
ResponseEntity<User> response = null;
try {
response = restOperations.exchange(Constants.ENDPOIT_SEARCH_USER, HttpMethod.GET, new HttpEntity<Object>(Utils.createHeaders()), User.class);
logger.info("=== RESPONSE ===" + response + " === ");
} catch (Exception e) {
logger.error("=== ERROR === " + e.getMessage() + " === ");
}
return user;
}
}
Project-C:
UserService.java
package com.neweraed.rest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.neweraed.datamodel.User;
import com.neweraed.services.midpoint.UserOperations;
#RestController
#RequestMapping(value="/api/v1/users")
public class UserService {
#RequestMapping(value="/", method=RequestMethod.POST)
public User addUser(#RequestBody User user) {
UserOperations userOperations = new UserOperations();
return userOperations.addUser(user);
}
}
It seems like everything is OK on my pom.xml files. How can I solve this?
According to your premises, pom.xml of the project B must be:
<dependency>
<groupId>com.mygroupid</groupId>
<artifactId>project-A</artifactId>
<version>${project.version}</version>
</dependency>
And project C doesn't need the dependency with A because it is defined on B
Related
I am using Spring Boot 3.0.0 , Spring Cloud 2021.0.5 . I have BillingServiceApplication.java
package org.sid.billingservice;
import org.sid.billingservice.entities.Bill;
import org.sid.billingservice.entities.ProductItem;
import org.sid.billingservice.model.Customer;
import org.sid.billingservice.model.Product;
import org.sid.billingservice.repository.BillRepository;
import org.sid.billingservice.repository.ProductItemRepository;
import org.sid.billingservice.services.CustomerRestClient;
import org.sid.billingservice.services.ProductRestClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import java.util.Collection;
import java.util.Date;
import java.util.Random;
#SpringBootApplication
#EnableFeignClients
#ImportAutoConfiguration({FeignAutoConfiguration.class})
public class BillingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BillingServiceApplication.class, args);
}
#Bean
CommandLineRunner start(BillRepository billRepository, ProductItemRepository productItemRepository, CustomerRestClient customerRestClient, ProductRestClient productRestClient) {
return args -> {
Collection<Product> products = productRestClient.allProducts().getContent();
Long customerId = 1L;
Customer customer = customerRestClient.findCustomerById(customerId);
if (customer == null) throw new RuntimeException("Customer not found");
Bill bill = new Bill();
bill.setBillDate(new Date());
bill.setCustomerId(customerId);
Bill savedBill = billRepository.save(bill);
products.forEach(product -> {
ProductItem productItem = new ProductItem();
productItem.setBill(savedBill);
productItem.setProductId(product.getId());
productItem.setQuantity(1 + new Random().nextInt(10));
productItem.setPrice(product.getPrice());
productItem.setDiscount(Math.random());
productItemRepository.save(productItem);
});
};
}
}
my error
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
Log error full https://gist.github.com/donhuvy/348aa7b096cde63a7129ad0f009c7507
How to fix it?
please share your pom.xml. It looks like you don't have spring-cloud-starter-loadbalancer dependency in your project.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>2.4.4</version>
</dependency>
Replacing path with url in feign client class along with #ImportAutoConfiguration({FeignAutoConfiguration.class}) in main class worked for me.
I want to write a contract using spring cloud contract in producer API.
My controller looks like :
#PostMapping(value = "/employee")
public ResponseEntity<Employee> getEmployee(#RequestBody Request employeeRequest) {
Optional<Employee> employee = employeeService.getEmployee(employeeRequest);
if(employee.isPresent()){
return ResponseEntity.status(HttpStatus.OK).
contentType(MediaType.APPLICATION_JSON).body(employee.get());
}else{
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Request class :
#Setter
#Getter
public class Request {
private Integer id;
}
Response (Employee) class :
#Setter
#Getter
#AllArgsConstructor
#NoArgsConstructor
public class Employee {
public Integer id;
public String fname;
public String lname;
public Double salary;
public String gender;
}
Dsl written in groovy :
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method 'POST'
url '/employee'
headers {
contentType(applicationJson())
}
body(
id : 25
)
}
response {
status 200
headers {
contentType(applicationJson())
}
body("""
{
"id":25,
"fname":"sara",
"lname":"ahmadi",
"salary":"25000.00",
"gender":"F"
}
""")
}
}
Plugin in pom.xml :
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>2.2.3.RELEASE</version>
<extensions>true</extensions>
<configuration>
<baseClassForTests>example.co.ir.contractproducer.BaseTestClass</baseClassForTests>
<testFramework>JUNIT5</testFramework>
</configuration>
</plugin>
And BaseTestClass :
import io.restassured.config.EncoderConfig;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig;
import isc.co.ir.contractproducer.controller.EmployeeController;
import isc.co.ir.contractproducer.model.Employee;
import isc.co.ir.contractproducer.model.Request;
import isc.co.ir.contractproducer.service.EmployeeService;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder;
import java.util.Optional;
#SpringBootTest
public class BaseTestClass {
#Autowired
private EmployeeController employeeController;
#MockBean
private EmployeeService employeeService;
#BeforeEach
public void setup(){
Employee employee2=new Employee(25,"Adam","Brown",25000.0,"F");
Request request = new Request();
request.setId(25);
Mockito.when(employeeService.getEmployee(request)).thenReturn(Optional.of(employee2));
EncoderConfig encoderConfig = new EncoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false);
RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().encoderConfig(encoderConfig);
StandaloneMockMvcBuilder standaloneMockMvcBuilder
= MockMvcBuilders.standaloneSetup(employeeController);
RestAssuredMockMvc.standaloneSetup(standaloneMockMvcBuilder);
}
}
Generated test class:
import isc.co.ir.contractproducer.BaseTestClass;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import io.restassured.module.mockmvc.specification.MockMvcRequestSpecification;
import io.restassured.response.ResponseOptions;
import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.*;
import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;
#SuppressWarnings("rawtypes")
public class ContractVerifierTest extends BaseTestClass {
#Test
public void validate_shouldReturnEmployee() throws Exception {
// given:
MockMvcRequestSpecification request = given()
.header("Content-Type", "application/json")
.body("{\"id\":25}");
// when:
ResponseOptions response = given().spec(request)
.post("/employee");
// then:
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.header("Content-Type")).matches("application/json.*");
// and:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).field("['id']").isEqualTo(25);
assertThatJson(parsedJson).field("['fname']").isEqualTo("sara");
assertThatJson(parsedJson).field("['lname']").isEqualTo("ahmadi");
assertThatJson(parsedJson).field("['salary']").isEqualTo("25000.00");
assertThatJson(parsedJson).field("['gender']").isEqualTo("F");
}
}
Now when I build the project I get this error :
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.469 s <<< FAILURE! - in example.co.ir.contractproducer.ContractVerifierTest
[ERROR] validate_shouldReturnEmployee Time elapsed: 0.607 s <<< FAILURE!
org.opentest4j.AssertionFailedError:
Expecting:
<500>
to be equal to:
<200>
but was not.
When I build project using Junit4 everything is work fine but while using Junit5 I had this problem.
How can I fix this problem?
Please remove #SpringBootTest and use RestAssuredMockMvc.standaloneSetup or leave #SpringBootTest, inject WebApplicationContext and use RestAssuredMockMvc.webAppContextSetup(context)
I've encountered a problem with Jackson serialization for my LoginResp object.
It shows me
Infinite recursion (StackOverflowError) (through reference chain: java.util.logging.LogManager["systemContext"]->java.util.logging.SystemLoggerContext["namedLoggers"]->java.util.Hashtable["global"]->java.util.logging.LoggerWeakRef["referent"]->java.util.logging.Logger["manager"]->java.util.logging.LogManager["systemContext"]->java.util.logging.SystemLoggerContext["namedLoggers"]->java.util.Hashtable["global"]->java.util.logging.LoggerWeakRef["referent"]->java.util.logging.Logger["manager"]->java.util.logging.LogManager["systemContext"]->java.util.logging.SystemLoggerContext["namedLoggers"]->java.util.Hashtable["global"]->java.util.logging.LoggerWeakRef["referent"]-.....
I'm trying to replicate the same scenario and using the same code , as mentioned in this link. But getting the above error. Please help, as I simply wants to write the value of my "resp" bean, to my "someJsonString" variable. Thanks in advance.
These are my dependencies
<properties>
<servlet-api-version>3.1.0</servlet-api-version>
<spring-webmvc-version>4.2.4.RELEASE</spring-webmvc-version>
<jackson-version>2.6.4</jackson-version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-webmvc-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
My LoginResp class
package my.beans.resp;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
#Component
#Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class LoginResp {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
My REST controller
package my.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import my.beans.req.LoginReqB;
import my.beans.resp.LoginResp;
#RestController
#RequestMapping(path = "/login")
public class LoginC {
#Autowired
private LoginResp resp;
#RequestMapping(method = RequestMethod.POST)
public String authenticateUserLogin(#RequestBody LoginReqB request) {
resp.setUsername("abc");
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
SimpleFilterProvider filterP = new SimpleFilterProvider().addFilter("loginResp",
SimpleBeanPropertyFilter.filterOutAllExcept("username"));
mapper.setFilterProvider(filterP);
String someJsonString = "";
try {
someJsonString = mapper.writeValueAsString(resp);
} catch (JsonProcessingException e) {
System.out.println(e.getMessage());
}
return someJsonString;
}
}
I solved it.
Observation: Jackson is not able to convert/process a proxy object at line
someJsonString = mapper.writeValueAsString(resp); in try block.
Solution: As proxy objects are wrapped around the actual object. I'm accessing the actual object behind the proxy using the function mentioned here.
Effect: Now, no need to customize my ObjectMapper, i.e.
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
SimpleFilterProvider filterP = new SimpleFilterProvider().addFilter("loginResp",
SimpleBeanPropertyFilter.filterOutAllExcept("username"));
mapper.setFilterProvider(filterP);
is no longer required.
First of all, don't use System.out.println method on the server side. Always use some logging utility such as log4j slf4j or any other one you might know. However, this is very unlikely a reason for your problem. Judging by the error, you have some circular reference with your logging configuration.Set a break point in your controller to see if you get there before the error occurs or the error occurs even before you attempt to invoke your method. In any case my guess is that you have logging configuration problem and your problem is the result of that and not of your code, which seems to be OK at first glance except System.out.println usage.
I need to create a report capability(function) in an existing spring boot web application. The suggestion was to use BIRT, which I could integrate with the spring boot web app.
I found the article below and was able to run the reports in a spring boot starter project (using http://start.spring.io/). This article, fairly old, did help me to get to a working example. https://spring.io/blog/2012/01/30/spring-framework-birt. This article is basically exactly what I want, but in a spring boot web app.
The challenge I have is to run the report using the BIRT viewer, which comes with nice additional features. (Print, Expoet data, PDF, pagination etc.)
I cannot find any spring boot example using BIRT the way this article describes.
My questions are:
Is there an alternative or other way to do reports in a spring boot web application? (obviously not want to re-inventing the wheel by creating BIRT like capability from scratch or run the report engine separate from the web application if at all possible)
Does anyone today have BIRT working (with the viewer) in a spring boot web application and be willing to share or educate me on the best way to do this?
(I tried to get JSP page working with spring boot, but unable to so successfully...more a lack of experience than anything else)
Can someone help me please.
Kind Regards,
Henk
Dependencies, an instance of the IReportEngine, and directories are the most challenging part of setting up BIRT in a spring boot web application.
This example has a little bit of error handling and depends on environment variables for specifying paths to important directories.
One nice things about this setup is that it is a standalone ReST API for building and generating reports. As a side benefit it doesn't depend on JSP at all.
I run it in a Docker container.
Originally there was a plan to use some of the interfaces to try other reporting frameworks like Jasper reports or something else.
This isn't exactly what you are asking for though in my opinion it is better in certain contexts. You have a lot of flexibility with using, configuring, and deploying the BIRT report runner as an application like this. This does not use the pre-packaged BIRT Viewer provided by Actian.
I would make a data container for reports called birt-report-runner-data and then put all of the directories for logging and reports on that container which you can then mount onto your BIRT container.
Main Components
Main File (BirtReportRunnerApplication.java)
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class BirtReportRunnerApplication {
public static void main(String[] args) {
SpringApplication.run(BirtReportRunnerApplication.class, args);
}
}
Controller to receive report Requests (ReportController.java)
package com.example.core.web.controller;
import com.example.core.model.BIRTReport;
import com.example.core.service.ReportRunner;
import com.example.core.web.dto.ReportRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController("ReportController")
#RequestMapping("/reports")
public class ReportController {
private Logger logger = LoggerFactory.getLogger(ReportController.class);
#Autowired
#Qualifier("birt")
ReportRunner reportRunner;
#RequestMapping(value = "/birt", method = RequestMethod.POST)
public ResponseEntity<byte[]> getBIRTReport(#RequestBody ReportRequest reportRequest) {
byte[] reportBytes;
ResponseEntity<byte[]> responseEntity;
try {
logger.info("REPORT REQUEST NAME: " + reportRequest.getReportName());
reportBytes =
new BIRTReport(
reportRequest.getReportName(),
reportRequest.getReportParameters(),
reportRunner)
.runReport().getReportContent().toByteArray();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.parseMediaType("application/pdf"));
String fileName = reportRequest.getReportName() + ".pdf";
httpHeaders.setContentDispositionFormData(fileName, fileName);
httpHeaders.setCacheControl("must-revalidate, post-check=0, pre-check=0");
responseEntity = new ResponseEntity<byte[]>(reportBytes, httpHeaders, HttpStatus.OK);
} catch (Exception e) {
responseEntity = new ResponseEntity<byte[]>(HttpStatus.NOT_IMPLEMENTED);
return responseEntity;
}
return responseEntity;
}
}
Report Request DTO for specifying report to run (ReportRequest.java)
package com.example.core.web.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
public class ReportRequest {
private String reportName;
private String reportParameters;
public ReportRequest(#JsonProperty("reportName") String reportName,
#JsonProperty("reportParameters") String reportParameters) {
this.reportName = reportName;
this.reportParameters = reportParameters;
}
public String getReportName() {
return reportName;
}
public String getReportParameters() {
return reportParameters;
}
public void setReportParameters(String reportParameters) {
this.reportParameters = reportParameters;
}
}
Interface for report runner (ReportRunner.java)
package com.example.core.service;
import com.example.core.model.Report;
import java.io.ByteArrayOutputStream;
public interface ReportRunner {
ByteArrayOutputStream runReport(Report report);
}
Largest class that does most of the work (BIRTReportRunner.java)
package com.example.core.service;
import com.example.core.model.Report;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.*;
import org.eclipse.core.internal.registry.RegistryProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
#Service
#Qualifier("birt")
public class BIRTReportRunner implements ReportRunner {
private static final String DEFAULT_LOGGING_DIRECTORY = "defaultBirtLoggingDirectory/";
private Logger logger = LoggerFactory.getLogger(BIRTReportRunner.class);
private static String reportOutputDirectory;
private IReportEngine birtReportEngine = null;
#Autowired
private Environment env;
/**
* Starts up and configures the BIRT Report Engine
*/
#PostConstruct
public void startUp() {
if(env.getProperty("birt_report_input_dir") == null)
throw new RuntimeException("Cannot start application since birt report input directory was not specified.");
try {
String birtLoggingDirectory = env.getProperty("birt_logging_directory") == null ? DEFAULT_LOGGING_DIRECTORY : env.getProperty("birt_logging_directory");
Level birtLoggingLevel = env.getProperty("birt_logging_level") == null ? Level.SEVERE : Level.parse(env.getProperty("birt_logging_level"));
EngineConfig engineConfig = new EngineConfig();
logger.info("BIRT LOG DIRECTORY SET TO : {}", birtLoggingDirectory);
logger.info("BIRT LOGGING LEVEL SET TO {}", birtLoggingLevel);
engineConfig.setLogConfig(birtLoggingDirectory, birtLoggingLevel);
// Required due to a bug in BIRT that occurs in calling Startup after the Platform has already been started up
RegistryProviderFactory.releaseDefault();
Platform.startup(engineConfig);
IReportEngineFactory reportEngineFactory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
birtReportEngine = reportEngineFactory.createReportEngine(engineConfig);
} catch (BirtException e) {
// TODO add logging aspect and find out how to log a platform startup problem from this catch block, if possible, using the aspect.
// Possibly rethrow the exception here and catch it in the aspect.
logger.error("Birt Startup Error: {}", e.getMessage());
}
reportOutputDirectory = env.getProperty("birt_temp_file_output_dir");
}
/**
* Shuts down the BIRT Report Engine
*/
#PreDestroy
public void shutdown() {
birtReportEngine.destroy();
RegistryProviderFactory.releaseDefault();
Platform.shutdown();
}
public File getReportFromFilesystem(String reportName) throws RuntimeException {
String reportDirectory = env.getProperty("birt_report_input_dir");
Path birtReport = Paths.get(reportDirectory + File.separator + reportName + ".rptdesign");
if(!Files.isReadable(birtReport))
throw new RuntimeException("Report " + reportName + " either did not exist or was not writable.");
return birtReport.toFile();
}
/**
* This method creates and executes the report task, the main responsibility
* of the entire Report Service.
* This method is key to enabling pagination for the BIRT report. The IRunTask run task
* is created and then used to generate an ".rptdocument" binary file.
* This binary file is then read by the separately created IRenderTask render
* task. The render task renders the binary document as a binary PDF output
* stream which is then returned from the method.
* <p>
*
* #param birtReport the report object created at the controller to hold the data of the report request.
* #return Returns a ByteArrayOutputStream of the PDF bytes generated by the
*/
#Override
public ByteArrayOutputStream runReport(Report birtReport) {
ByteArrayOutputStream byteArrayOutputStream;
File rptDesignFile;
// get the path to the report design file
try {
rptDesignFile = getReportFromFilesystem(birtReport.getName());
} catch (Exception e) {
logger.error("Error while loading rptdesign: {}.", e.getMessage());
throw new RuntimeException("Could not find report");
}
// process any additional parameters
Map<String, String> parsedParameters = parseParametersAsMap(birtReport.getParameters());
byteArrayOutputStream = new ByteArrayOutputStream();
try {
IReportRunnable reportDesign = birtReportEngine.openReportDesign(rptDesignFile.getPath());
IRunTask runTask = birtReportEngine.createRunTask(reportDesign);
if (parsedParameters.size() > 0) {
for (Map.Entry<String, String> entry : parsedParameters.entrySet()) {
runTask.setParameterValue(entry.getKey(), entry.getValue());
}
}
runTask.validateParameters();
String rptdocument = reportOutputDirectory + File.separator
+ "generated" + File.separator
+ birtReport.getName() + ".rptdocument";
runTask.run(rptdocument);
IReportDocument reportDocument = birtReportEngine.openReportDocument(rptdocument);
IRenderTask renderTask = birtReportEngine.createRenderTask(reportDocument);
PDFRenderOption pdfRenderOption = new PDFRenderOption();
pdfRenderOption.setOption(IPDFRenderOption.REPAGINATE_FOR_PDF, new Boolean(true));
pdfRenderOption.setOutputFormat("pdf");
pdfRenderOption.setOutputStream(byteArrayOutputStream);
renderTask.setRenderOption(pdfRenderOption);
renderTask.render();
renderTask.close();
} catch (EngineException e) {
logger.error("Error while running report task: {}.", e.getMessage());
// TODO add custom message to thrown exception
throw new RuntimeException();
}
return byteArrayOutputStream;
}
/**
* Takes a String of parameters started by '?', delimited by '&', and with
* keys and values split by '=' and returnes a Map of the keys and values
* in the String.
*
* #param reportParameters a String from a HTTP request URL
* #return a map of parameters with Key,Value entries as strings
*/
public Map<String, String> parseParametersAsMap(String reportParameters) {
Map<String, String> parsedParameters = new HashMap<String, String>();
String[] paramArray;
if (reportParameters.isEmpty()) {
throw new IllegalArgumentException("Report parameters cannot be empty");
} else if (!reportParameters.startsWith("?") && !reportParameters.contains("?")) {
throw new IllegalArgumentException("Report parameters must start with a question mark '?'!");
} else {
String noQuestionMark = reportParameters.substring(1, reportParameters.length());
paramArray = noQuestionMark.split("&");
for (String param : paramArray) {
String[] paramGroup = param.split("=");
if (paramGroup.length == 2) {
parsedParameters.put(paramGroup[0], paramGroup[1]);
} else {
parsedParameters.put(paramGroup[0], "");
}
}
}
return parsedParameters;
}
}
Report object class (Report.java)
package com.example.core.model;
import com.example.core.service.ReportRunner;
import java.io.ByteArrayOutputStream;
import java.util.List;
/**
* A Report object has a byte representation of the report output that can be
* used to write to any output stream. This class is designed around the concept
* of using ByteArrayOutputStreams to write PDFs to an output stream.
*
*
*/
public abstract class Report {
protected String name;
protected String parameters;
protected ByteArrayOutputStream reportContent;
protected ReportRunner reportRunner;
public Report(String name, String parameters, ReportRunner reportRunner) {
this.name = name;
this.parameters = parameters;
this.reportRunner = reportRunner;
}
/**
* This is the processing method for a Report. Once the report is ran it
* populates an internal field with a ByteArrayOutputStream of the
* report content generated during the run process.
* #return Returns itself with the report content output stream created.
*/
public abstract Report runReport();
public ByteArrayOutputStream getReportContent() {
return this.reportContent;
}
public String getName() {
return name;
}
public String getParameters() {
return parameters;
}
}
BIRTReport object class (BIRTReport.java)
package com.example.core.model;
import com.example.core.service.ReportRunner;
public class BIRTReport extends Report {
public BIRTReport(String name, String reportParameters, ReportRunner reportRunner) {
super(name, reportParameters, reportRunner);
}
#Override
public Report runReport() {
this.reportContent = reportRunner.runReport(this);
return this;
}
}
Build file (build.gradle)
buildscript {
ext {
springBootVersion = '1.3.2.RELEASE'
}
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'com.example.simple-birt-runner'
version = '1.0.0'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext {
springBootVersion = '1.3.0.M5'
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
// BIRT Runtime and ReportEngine dependencies
// These were pulled from the Actuate download site at http://download.eclipse.org/birt/downloads/ for version 4.5.0
compile fileTree(dir: 'lib', include: '*.jar')
/*compile("org.eclipse.birt.runtime:org.eclipse.birt.runtime:4.4.2") {
exclude group: "org.eclipse.birt.runtime", module: "org.apache.poi"
exclude group: "org.eclipse.birt.runtime", module: "org.eclipse.orbit.mongodb"
}*/
compile("org.springframework.boot:spring-boot-starter-tomcat")
// include any runtime JDBC driver dependencies here
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Dependencies
Get all of the dependencies in the Birt Viewer lib directory and put them on your classpath. Here is a full list of external dependencies:
com.ibm.icu_54.1.1.v201501272100.jar
com.lowagie.text_2.1.7.v201004222200.jar
derby.jar
flute.jar
javax.wsdl_1.5.1.v201012040544.jar
javax.xml.stream_1.0.1.v201004272200.jar
javax.xml_1.3.4.v201005080400.jar
net.sourceforge.lpg.lpgjavaruntime_1.1.0.v201004271650.jar
org.apache.batik.bridge_1.6.0.v201011041432.jar
org.apache.batik.css_1.6.0.v201011041432.jar
org.apache.batik.dom.svg_1.6.0.v201011041432.jar
org.apache.batik.dom_1.6.1.v201505192100.jar
org.apache.batik.ext.awt_1.6.0.v201011041432.jar
org.apache.batik.parser_1.6.0.v201011041432.jar
org.apache.batik.pdf_1.6.0.v201105071520.jar
org.apache.batik.svggen_1.6.0.v201011041432.jar
org.apache.batik.transcoder_1.6.0.v201011041432.jar
org.apache.batik.util.gui_1.6.0.v201011041432.jar
org.apache.batik.util_1.6.0.v201011041432.jar
org.apache.batik.xml_1.6.0.v201011041432.jar
org.apache.commons.codec_1.6.0.v201305230611.jar
org.apache.commons.logging_1.1.1.v201101211721.jar
org.apache.lucene.core_3.5.0.v20120725-1805.jar
org.apache.poi_3.9.0.v201405241750.jar
org.apache.xerces_2.9.0.v201101211617.jar
org.apache.xml.resolver_1.2.0.v201005080400.jar
org.apache.xml.serializer_2.7.1.v201005080400.jar
org.eclipse.birt.runtime_4.5.0.jar
org.eclipse.core.contenttype_3.5.0.v20150421-2214.jar
org.eclipse.core.expressions_3.5.0.v20150421-2214.jar
org.eclipse.core.filesystem_1.5.0.v20150421-0713.jar
org.eclipse.core.jobs_3.7.0.v20150330-2103.jar
org.eclipse.core.resources_3.10.0.v20150423-0755.jar
org.eclipse.core.runtime.compatibility_3.2.300.v20150423-0821.jar
org.eclipse.core.runtime_3.11.0.v20150405-1723.jar
org.eclipse.datatools.connectivity.apache.derby.dbdefinition_1.0.2.v201107221459.jar
org.eclipse.datatools.connectivity.apache.derby_1.0.103.v201212070447.jar
org.eclipse.datatools.connectivity.console.profile_1.0.10.v201109250955.jar
org.eclipse.datatools.connectivity.db.generic_1.0.1.v201107221459.jar
org.eclipse.datatools.connectivity.dbdefinition.genericJDBC_1.0.2.v201310181001.jar
org.eclipse.datatools.connectivity.oda.consumer_3.2.6.v201403131814.jar
org.eclipse.datatools.connectivity.oda.design_3.3.6.v201403131814.jar
org.eclipse.datatools.connectivity.oda.flatfile_3.1.8.v201403010906.jar
org.eclipse.datatools.connectivity.oda.profile_3.2.9.v201403131814.jar
org.eclipse.datatools.connectivity.oda_3.4.3.v201405301249.jar
org.eclipse.datatools.connectivity.sqm.core_1.2.8.v201401230755.jar
org.eclipse.datatools.connectivity_1.2.11.v201401230755.jar
org.eclipse.datatools.enablement.hsqldb.dbdefinition_1.0.0.v201107221502.jar
org.eclipse.datatools.enablement.hsqldb_1.0.0.v201107221502.jar
org.eclipse.datatools.enablement.ibm.db2.iseries.dbdefinition_1.0.3.v201107221502.jar
org.eclipse.datatools.enablement.ibm.db2.iseries_1.0.2.v201107221502.jar
org.eclipse.datatools.enablement.ibm.db2.luw.dbdefinition_1.0.7.v201405302027.jar
org.eclipse.datatools.enablement.ibm.db2.luw_1.0.3.v201401170830.jar
org.eclipse.datatools.enablement.ibm.db2.zseries.dbdefinition_1.0.4.v201107221502.jar
org.eclipse.datatools.enablement.ibm.db2.zseries_1.0.2.v201107221502.jar
org.eclipse.datatools.enablement.ibm.db2_1.0.0.v201401170830.jar
org.eclipse.datatools.enablement.ibm.informix.dbdefinition_1.0.4.v201107221502.jar
org.eclipse.datatools.enablement.ibm.informix_1.0.1.v201107221502.jar
org.eclipse.datatools.enablement.ibm_1.0.0.v201401170830.jar
org.eclipse.datatools.enablement.msft.sqlserver.dbdefinition_1.0.1.v201201240505.jar
org.eclipse.datatools.enablement.msft.sqlserver_1.0.3.v201308161009.jar
org.eclipse.datatools.enablement.mysql.dbdefinition_1.0.4.v201109022331.jar
org.eclipse.datatools.enablement.mysql_1.0.4.v201212120617.jar
org.eclipse.datatools.enablement.oda.ws_1.2.6.v201403131825.jar
org.eclipse.datatools.enablement.oda.xml_1.2.5.v201403131825.jar
org.eclipse.datatools.enablement.oracle.dbdefinition_1.0.103.v201206010214.jar
org.eclipse.datatools.enablement.oracle_1.0.0.v201107221506.jar
org.eclipse.datatools.enablement.postgresql.dbdefinition_1.0.2.v201110070445.jar
org.eclipse.datatools.enablement.postgresql_1.1.1.v201205252207.jar
org.eclipse.datatools.enablement.sap.maxdb.dbdefinition_1.0.0.v201107221507.jar
org.eclipse.datatools.enablement.sap.maxdb_1.0.0.v201107221507.jar
org.eclipse.datatools.modelbase.dbdefinition_1.0.2.v201107221519.jar
org.eclipse.datatools.modelbase.derby_1.0.0.v201107221519.jar
org.eclipse.datatools.modelbase.sql.query_1.1.4.v201212120619.jar
org.eclipse.datatools.modelbase.sql_1.0.6.v201208230744.jar
org.eclipse.datatools.sqltools.data.core_1.2.3.v201212120623.jar
org.eclipse.datatools.sqltools.parsers.sql.lexer_1.0.1.v201107221520.jar
org.eclipse.datatools.sqltools.parsers.sql.query_1.2.1.v201201250511.jar
org.eclipse.datatools.sqltools.parsers.sql_1.0.2.v201107221520.jar
org.eclipse.datatools.sqltools.result_1.1.6.v201402080246.jar
org.eclipse.emf.common_2.11.0.v20150512-0501.jar
org.eclipse.emf.ecore.change_2.11.0.v20150512-0501.jar
org.eclipse.emf.ecore.xmi_2.11.0.v20150512-0501.jar
org.eclipse.emf.ecore_2.11.0.v20150512-0501.jar
org.eclipse.equinox.app_1.3.300.v20150423-1356.jar
org.eclipse.equinox.common_3.7.0.v20150402-1709.jar
org.eclipse.equinox.preferences_3.5.300.v20150408-1437.jar
org.eclipse.equinox.registry_3.6.0.v20150318-1503.jar
org.eclipse.help_3.6.0.v20130326-1254.jar
org.eclipse.osgi.services_3.5.0.v20150519-2006.jar
org.eclipse.osgi_3.10.100.v20150529-1857.jar
org.eclipse.update.configurator_3.3.300.v20140518-1928.jar
org.mozilla.javascript_1.7.5.v201504281450.jar
org.w3c.css.sac_1.3.1.v200903091627.jar
org.w3c.dom.events_3.0.0.draft20060413_v201105210656.jar
org.w3c.dom.smil_1.0.1.v200903091627.jar
org.w3c.dom.svg_1.1.0.v201011041433.jar
Application properties (application.yml)
birt:
report:
output:
dir: ${birt_temp_file_output_dir}
input:
dir: ${birt_report_input_dir}
logging:
level: ${birt_logging_level}
server:
port: 8080
context-path: /simple-birt-runner
birt:
logging:
level: SEVERE
logging:
level:
org:
springframework:
web: ERROR
Dockerfile for running (Dockerfile)
FROM java:openjdk-8u66-jre
MAINTAINER Kent O. Johnson <kentoj#gmail.com>
COPY com.example.simple-birt-runner-*.jar /opt/soft/simple-birt-runner.jar
RUN mkdir -p /reports/input \
&& mkdir /reports/output \
&& mkdir -p /reports/log/engine
WORKDIR /opt/soft
CMD java -jar -Xms128M -Xmx4G simple-birt-runner.jar
EXPOSE 8080
Docker compose file for running image (docker-compose.yml)
simple-birt-runner:
image: soft/simple-birt-runner-release
ports:
- "8090:8080"
environment:
- birt_temp_file_output_dir=/reports/output
- birt_report_input_dir=/reports/input
- birt_logging_directory=/reports/log/engine
- birt_logging_level=SEVERE
volumes_from:
- birt-report-runner-data
Regarding #Kent Johnson 's answer. I didn't manage to configure the project with gradle, but I managed to build it with maven.
Below is the pom.xml
<?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>birt-runner</groupId>
<artifactId>com.example.simple-birt-runner</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.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-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.birt.runtime/org.eclipse.birt.runtime -->
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.birt.runtime</artifactId>
<version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I'm a newbie to mashape unirest, and I can't seem to figure out what I'm doing wrong.
I'm using maven to use unirest as a dependency, as so:
<dependencies>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.3.27</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
</dependencies>
Next, I'm trying to use unirest to make an asyncronous call to my server using java anonymous callbacks.
import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.lang.*;
import java.io.*;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.http.options.Options;
public class CRDTClient {
private ArrayList<String> servers;
public AtomicInteger successCount;
public CRDTClient() {
servers = new ArrayList(3);
servers.add("http://localhost:3000");
servers.add("http://localhost:3001");
servers.add("http://localhost:3002");
}
public boolean put(long key, String value) {
final CountDownLatch countDownLatch = new CountDownLatch(servers.size());
successCount = new AtomicInteger();
for (final String serverUrl : servers) {
Future<HttpResponse<JsonNode>> future = Unirest.post(serverUrl + "/cache/{key}/{value}")
.header("accept", "application/json")
.routeParam("key", Long.toString(key))
.routeParam("value", value).asJson()
.asJsonAsync(new Callback<JsonNode>() {
public void failed(UnirestException e) {
System.out.println("The request has failed: " + serverUrl);
countDownLatch.countDown();
}
public void completed(HttpResponse<JsonNode> response) {
int code = response.getStatus();
if (code == 200) {
successCount.incrementAndGet();
}
countDownLatch.countDown();
}
public void cancelled() {
System.out.println("The request has been cancelled");
}
});
}
// Block the thread until all responses gotten
countDownLatch.await();
return (Math.round(successCount.floatValue() / servers.size()) == 1);
}
The error I get when I run
mvn clean package -e
is
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project client: Compilation failure
[ERROR] /Users/jonguan/Documents/SJSU/cmpe273/cmpe273-lab4/client/src/main/java/edu/sjsu/cmpe/cache/client/CRDTClient.java:[40,20] error: cannot find symbol
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project client: Compilation failure
.../cmpe/cache/client/CRDTClient.java:[40,20] error: cannot find symbol
which turns out to be the line on .asJsonAsync
I tried importing all sorts of classes, but it doesn't seem to work.
I figured it out. Stupid me.
On the line previous, there is an extra .asJson
.routeParam("value", value).asJson()
should just be
.routeParam("value", value)