liquibase:diff not giving me expected result - spring-data-jpa

I have a JPA entity called customer and goes like this
#Entity
public class Customer {
private int custNo;
private String custName;
private String country;
public Customer() {
}
public Customer(int custNumber, String custName, String country) {
this.custNo = custNumber;
this.custName = custName;
this.country = country;
}
public int getCustNo() {
return custNo;
}
public void setCustNo(int custNo) {
this.custNo = custNo;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
and my db has 2 tables :- BE132_name and BE1jj231_address ,
I am running my profile liquibase:diff and is giving me the change set as follows
<changeSet author="jobs (generated)" id="1554122585461-10">
<dropTable tableName="BE132_name"/>
</changeSet>
<changeSet author="jobs (generated)" id="1554122585461-11">
<dropTable tableName="BE1jj231_address"/>
</changeSet>
As you can see it created drop table since I dont have its corresponding JPA entities. But why is it not creating the create script for my Customer ?
For an empty data base (one without any tables) , I am getting
INFO 4/2/19 5:47 PM: liquibase: No changes found, nothing to do

I used the liquibase-hibernate plugin for this!. It is capable of generating the changeset for a JPA entity even if its corresponding table is not there in the db.
The plugin
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate4</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.3.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
and the liquibase.properties
changeLogFile=classpath:liquibase-changeLog.xml
url=jdbc:mysql://localhost:3306/oauth_reddit
username=tutorialuser
password=tutorialmy5ql
driver=com.mysql.jdbc.Driver
referenceUrl=hibernate:spring:org.baeldung.persistence.model
?dialect=org.hibernate.dialect.MySQLDialect
diffChangeLogFile=src/main/resources/liquibase-diff-changeLog.xml
The referenceUrl is using package scan, so the dialect parameter is required. changeLogFile is the location of changeset for which the db is in sync. diffChangeLogFile is the location where the difference changelog has to be flushed.

Related

couldn't resolve the issue "Consider defining a bean named 'mongoTemplate' in your configuration." in spring data

I am building a simple spring boot application to save some data into Mongo. I am stuck with the issue "Consider defining a bean named 'mongoTemplate' in your configuration.". I tried many things. But nothing works. Below is the code.
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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ex</groupId>
<artifactId>mongo-export</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mongo-export</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Main App
//#EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
//#SpringBootApplication(exclude = {MongoAutoConfiguration.class,
//MongoDataAutoConfiguration.class})
#EnableMongoRepositories(basePackageClasses = StudentsRepository.class)
#SpringBootApplication
#ComponentScan("com.ex")
public class MongoExportApplication implements CommandLineRunner {
#Autowired
StudentsRepository studentsRepository;
public static void main(String[] args) {
SpringApplication.run(MongoExportApplication.class, args);
}
#Override
public void run(String... args) {
studentsRepository.save(new Student(UUID.randomUUID(), 123, "Student1"));
System.out.println("~~~~~~~~~~~~~Saved the student object~~~~~~~~~~~~~");
}
}
Student Bean
#Document("students")
public class Student {
#Id
private UUID id;
private int rollNumber;
private String name;
public Student(UUID id, int rollNumber, String name) {
this.id = id;
this.rollNumber = rollNumber;
this.name = name;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Repository
public interface StudentsRepository extends MongoRepository<Student, UUID> {
}
application.properties
spring.data.mongodb.uri=mongodb+srv://username:pwd.url/dataBaseName?retryWrites=true&w=majority
I used the actual username, password, url, databaseName in the above value
Trace
***************************
APPLICATION FAILED TO START
***************************
Description:
Field studentsRepository in
com.ex.mongoexport.MongoExportApplication required a bean named
'mongoTemplate' that could not be found.
The injection point has the following annotations:
-
#org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'mongoTemplate' in your configuration.
Process finished with exit code 1

NetBeans Junit5 Tests-Output ignore DisplayName Nested-Format

I do not get the names from the annoations "#DisplayName" as a test result in NetBeans. Only the names of the test functions are shown. The grouped display of the nested tests is also ignored.
Source code
#DisplayName("test facade")
public class TestFacadeTest {
#BeforeAll
public static void setUpClass() {
}
#AfterAll
public static void tearDownClass() {
}
#BeforeEach
public void setUp() {
}
#AfterEach
public void tearDown() {
}
#Test
#DisplayName("senseless test")
public void test(){
Assertions.assertTrue(true);
}
#Nested
#DisplayName("tests - compareStringNullSave")
class CompateStringNullSaveTestGroup {
#BeforeEach
public void setUp() {
}
#Test
#DisplayName("both identical")
public void Test1(){
String str1 = "Test123";
String str2 = "Test123";
Assertions.assertTrue(TestFacade.compareStringNullSave(str1, str2));
Assertions.assertTrue(TestFacade.compareStringNullSave(str2, str1));
}
#Test
#DisplayName("Identical text but different uppercase and lowercase letters")
public void Test2(){
String str1 = "Test123";
String str2 = "test123";
Assertions.assertFalse(TestFacade.compareStringNullSave(str1, str2));
Assertions.assertFalse(TestFacade.compareStringNullSave(str2, str1));
}
}
}
Extract from the pom.xml
<dependencies>
[...]
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
<build>
<plugins>
[...]
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<properties>
<configurationParameters>
junit.jupiter.conditions.deactivate = *
junit.jupiter.extensions.autodetection.enabled = true
junit.jupiter.testinstance.lifecycle.default = per_class
</configurationParameters>
</properties>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
What do I have to set so that the test display in NetBeans shows the DisplayNames and uses the nested grouping?
NetBeans version: 12.0
Java version: 11 (OpenJDK)
Maven added support for the #Display annotation in the 3.0.0.0-M4 version of the surefire plugin. Also, you need to configure the parameters of statelessTestsetReporter and statelessTestsetInfoReporter extensions to allow for NetBeans to match display names correctly.
So currently (01.2021), you can use 3.0.0.0-M4 or newer versions of the surefire plugin, and the version of JUnit should be more than 5.5.2. Something like that should work:
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<version>3.0</version>
<usePhrasedFileName>true</usePhrasedFileName>
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
</statelessTestsetReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoReporter">
<usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
</statelessTestsetInfoReporter>
<properties>
<configurationParameters>
junit.jupiter.conditions.deactivate = *
junit.jupiter.extensions.autodetection.enabled = true
junit.jupiter.testinstance.lifecycle.default = per_class
</configurationParameters>
</properties>
</configuration>
</plugin>
</plugins>
Here how it looks in my case:
#DisplayName("Display Name of class")
public class testClassSimple {
#Test
#DisplayName("Display name of method 1")
public void testMethod1() {
}
#Test
#DisplayName("Display name of method 2")
public void testMethod2() {
}
}
As for nested grouping and the #Nested annotation, currently, NetBeans doesn't support nested grouping of test results in the test result window. Probably it's better to use different classes for such tests instead of nested classes.

Hazelcast Repository Still queries the Database

I am working on a spring boot project. In that, i have an entity called ProductMap which i want to keep in cache. I did that using MapLoader and defining the configuration for the map as below.
#Bean
public Config hazelcastConfig() {
return new Config().setInstanceName("hazelcast-instance").addMapConfig(
new MapConfig().setName("ProductMap")
.setMapStoreConfig(
new MapStoreConfig().setEnabled(true).setInitialLoadMode(MapStoreConfig.InitialLoadMode.EAGER)
.setClassName("com.hazelcast.example.HzTest.config.ProductMapLoader")
));
}
ProductMap entity:
#Data
#Entity
#KeySpace("ProductMap")
#Table
public class ProductMap implements Serializable {
#Id
#org.springframework.data.annotation.Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer Id;
private String name;
private Integer category;
private Integer productType;
}
ProductMapLoader:
#Log4j2
#Component
public class ProductMapLoader implements MapLoader<Integer, ProductMap>, ApplicationContextAware {
private static ProductMapRepository productMapRepository;
#Override
public synchronized ProductMap load(Integer integer) {
System.out.println("Load::" + integer);
return productMapRepository.findById(integer).get();
}
#Override
public synchronized Map<Integer, ProductMap> loadAll(Collection<Integer> collection) {
Map<Integer, ProductMap> result = new HashMap<>();
for (Integer key : collection) {
ProductMap productMap = this.load(key);
if (productMap != null) {
result.put(key, productMap);
}
}
return result;
}
#Override
public synchronized Iterable<Integer> loadAllKeys() {
System.out.println("load all keys" + productMapRepository);
return productMapRepository.findAllProdMapKeys();
}
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
productMapRepository = applicationContext.getBean(ProductMapRepository.class);
}
}
I am loading the cache on startup,
#PostConstruct
public void Init() {
IMap map = hazelcastInstance.getMap("ProductMap"); // this will load the cache
}
In also created a HazelcastRepository,
public interface ProductMapKvRepo extends KeyValueRepository<ProductMap, Integer> {
List<ProductMap> findByProductType(Integer productType);
}
In one of my service methods, it calls productMapKvRepo.findAll() and productMapKvRepo.findByProductType(1). But the repository still queries the database.
Hibernate: select productmap0_.id as id1_0_, productmap0_.category as category2_0_, productmap0_.name as name3_0_, productmap0_.product_type as product_4_0_ from product_map productmap0_
Hibernate: select productmap0_.id as id1_0_, productmap0_.category as category2_0_, productmap0_.name as name3_0_, productmap0_.product_type as product_4_0_ from product_map productmap0_ where productmap0_.product_type=?
dependencies used:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.12.7</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>3.12.7</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>spring-data-hazelcast</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.13</version>
</dependency>
</dependencies>
Can anyone tell me what is wrong here and what can i do?
Your logs indicate that your Spring Data repository is Hibernate-backed which means your project is misconfigured. Spring Data Hazelcast doesn't use Hibernate to read data from the Hazelcast IMDG cluster.
If the use of Hibernate is intended(most likely), then you should consider using its native second-level cache capabilities with Hazelcast instead of wrapping Spring Data repositories inside a MapLoader. You can find the example here.
However, if you want to apply the read-through caching pattern with a MapLoader, you'd need to use a spring-data-hazelcast artifact to read data from Hazelcast cluster directly.
While I was working with Spring Boot v2.1.3.RELEASE and Spring v5.1.5.RELEASE , spring was injecting SimpleJpaRepository type for both repositories :
MyStandardRepository And MyHazelCastRepository, with the presence only of #EnableHazelcastRepositories
By adding also #EnableJpaRepositories to my HazelcastConfiguration class :
spring then injected SimpleKeyValueRepository bean type into MyHazelCastRepository :
#Configuration
#EnableHazelcastRepositories(basePackages = {"com.test.repository.hazelcast"})
#EnableJpaRepositories(basePackages = {"com.test.repository.dao"})
public class HazelcastConfiguration {
#Bean
HazelcastInstance hazelcastInstance() {
return Hazelcast.newHazelcastInstance();
}
#Bean
public KeyValueOperations keyValueTemplate() {
return new KeyValueTemplate(new HazelcastKeyValueAdapter(hazelcastInstance()));
}
#Bean
public HazelcastKeyValueAdapter hazelcastKeyValueAdapter(HazelcastInstance hzInstance) {
return new HazelcastKeyValueAdapter(hzInstance);
}
}

How can we create Auto generated field for mongodb using spring boot

I write some code.I want to make questionId field in BaseQuestion Class as Autogenerated.Any solution for that? I am not using jpa jar.so i can't use #Generatedvalue annotation.So how we show here this field is auto generated.
code is below.
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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>audit_project</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
BaseQuestion.java
package model;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "basequestion")
public class BaseQuestion {
#ID
private String id;
private int questionId;
private String responseType;
private boolean required;
private boolean active;
private String questionCode;
private QuestionText questionText;
private String category;
private List<Responses> responses;
public QuestionText getQuestionText() {
return questionText;
}
public void setQuestionText(QuestionText questionText) {
this.questionText = questionText;
}
public List<Responses> getResponses() {
return responses;
}
public void setResponses(List<Responses> responses) {
this.responses = responses;
}
public int getQuestionId() {
return questionId;
}
public void setQuestionId(int questionId) {
this.questionId = questionId;
}
public String getResponseType() {
return responseType;
}
public void setResponseType(String responseType) {
this.responseType = responseType;
}
public boolean getRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
public String getQuestionCode() {
return questionCode;
}
public void setQuestionCode(String questionCode) {
this.questionCode = questionCode;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
AuditProjectRepository.java
package repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import model.BaseQuestion;
public interface AuditProjectRepository extends MongoRepository<BaseQuestion, String> {
public BaseQuestion findByQuestionId(int questionId);
public BaseQuestion findByQuestionCode(String questionCode);
public Long deleteByQuestionId(int questionid);
}
MongoDB came with all sophisticated ObjectId generation feature, but often you just jumped the ship from relational database, and you still want an easy to read / communicate numeric identifier field which automatically increments every time new record is inserted.
One neat suggestion from MongoDB tutorial is to use a counter collection with a ‘counter name’ as its id, and a ‘seq’ field to store the last used number.
When developing using Spring Data MongoDB, this neat trick can be written as a simple service. Here I used the collection name as the counter name so it’s easy to guess / remember.
import static org.springframework.data.mongodb.core.FindAndModifyOptions.options;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.model.CustomSequences;
#Service
public class NextSequenceService {
#Autowired private MongoOperations mongo;
public int getNextSequence(String seqName)
{
CustomSequences counter = mongo.findAndModify(
query(where("_id").is(seqName)),
new Update().inc("seq",1),
options().returnNew(true).upsert(true),
CustomSequences.class);
return counter.getSeq();
}
}
CustomSequences is just a simple class representing the collection. Please beware the usage of int data type, this will limit to 2^31 entries maximum.
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "customSequences")
public class CustomSequences {
#Id
private String id;
private int seq;
// getters and setters
}
Then when inserting a new entry (with help of Spring MongoDB Repository support), just set the id field like this before you save it
BaseQuestion baseQuestion = new BaseQuestion();
baseQuestion.setQuestionId(nextSequenceService.getNextSequence("customSequences"));
/* Rest all values */
baseQuestionRepository.save(baseQuestion);
If you don't like this way then you need to use MongoDBEvents and use onBeforeConvert to generate automated value using same above approach.
Also above approach is threadsafe as findAndModify() is a thread safe atomic method
Using a String for your primary key would be a good idea. With this, you do not configure anything since the strings are automatically generated.

Excpetionmapper does not work with Wildfly Swarm but does work in Wildfly Server

I am trying to run a simple entity - controller - boundery application in WildFly Swarm. This works fine, however when I add an ExceptionMapper to catch the NotFoundException this does work on WildFly server 10 but not in WildFly swarm. Is this a bug in Shrinkwrap? Is this a bug in Wildfly Swarm? Or am I doing something wrong in Shrinkwrapping the deployment?
Here's the specific code for the Swarm Main class:
public class Main {
public static void main(String[] args) throws Exception {
Container container = new Container();
container.fraction(new DatasourcesFraction()
.jdbcDriver("h2", (d) -> {
d.driverClassName("org.h2.Driver");
d.xaDatasourceClass("org.h2.jdbcx.JdbcDataSource");
d.driverModuleName("com.h2database.h2");
})
.dataSource("demoDS", (ds) -> {
ds.driverName("h2");
ds.connectionUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
ds.userName("sa");
ds.password("sa");
})
);
// Prevent JPA Fraction from installing it's default datasource fraction
container.fraction(new JPAFraction()
.inhibitDefaultDatasource()
.defaultDatasource("jboss/datasources/demoDS")
);
container.start();
JAXRSArchive deployment = ShrinkWrap.create(JAXRSArchive.class);
deployment.addClasses(Customer.class);
deployment.addAsWebInfResource(new ClassLoaderAsset("META-INF/persistence.xml", Main.class.getClassLoader()), "classes/META-INF/persistence.xml");
deployment.addClass(CustomerService.class);
deployment.addResource(CustomerResource.class);
//BUG: The provider below is not working!
deployment.addClass(NotFoundExceptionMapper.class);
deployment.addAllDependencies();
container.deploy(deployment);
}
}
and here is my swarm demo 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>com.domain</groupId>
<artifactId>swarm-demo</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.wildfly-swarm.core>1.0.0.Beta2</version.wildfly-swarm.core>
<version.wildfly-swarm.plugin>1.0.0.Beta2</version.wildfly-swarm.plugin>
</properties>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs-weld</artifactId>
<version>${version.wildfly-swarm.core}</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>ejb</artifactId>
<version>${version.wildfly-swarm.core}</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jpa</artifactId>
<version>${version.wildfly-swarm.core}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly-swarm.plugin}</version>
<configuration>
<mainClass>nl.paston.swarm.demo.Main</mainClass>
<jvmArguments>
<!-- Needs to be tuned for performance -->
<jvmArgument>-Xmx128m</jvmArgument>
</jvmArguments>
</configuration>
<!-- Needed for fat jar creation -->
<executions>
<execution>
<id>package</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
For comparison, this is my pom.xml for the Wildfly server application:
<?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.domain</groupId>
<artifactId>wildfly-demo</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.java-ee>7.0</version.java-ee>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${version.java-ee}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
For completeness, here is the code for the ExceptionMapper:
#Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
#Override
public Response toResponse(NotFoundException e) {
System.out.println("Exceptionmapper called!");
return Response.ok().build();
}
}
The boundery:
#Path("/customer")
#ApplicationScoped
public class CustomerResource {
#Inject CustomerService cs;
#GET #Path("/") #Produces(MediaType.APPLICATION_JSON)
public List<Customer> get() {
return cs.getAll();
}
}
The controller:
#Stateless
public class CustomerService {
#PersistenceContext
private EntityManager em;
public List<Customer> getAll() {
return em.createNamedQuery(Customer.FIND_ALL, Customer.class).getResultList();
}
}
and the entity:
#Entity #Table(name = "CUSTOMER")
#NamedQueries({#NamedQuery(name = Customer.FIND_ALL, query = "SELECT c FROM Customer c")})
#XmlRootElement
public class Customer implements Serializable {
public static final String FIND_ALL = "Customer.findAll";
private static final long serialVersionUID = 1L;
#Id #GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Column(length = 40)
private String name;
public Customer() { }
public Customer(String name) { this.name = name; }
public int getId() { return id; }
protected void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
// toString, equals and hashCode overrides omitted
}
This has been resolved with the latest release. Please take a look at http://wildfly-swarm.io/posts/announcement-1-0-0-beta7/
Bear in mind that since Beta6 we've been providing a BOM with all the necessary versions of the different parts of WildFly Swarm, so importing the BOM into depMan will give you the right versions
I just tried with the latest swarm, and it doesn't seem to be an issue. Maybe you upgrade it'll work?
https://github.com/johnament/wildfly-swarm-examples/blob/master/jaxrs/jaxrs-shrinkwrap/src/main/java/org/wildfly/swarm/examples/jaxrs/shrinkwrap/Main.java
I suspect the issue has to do with your NotFoundException. What is the definition of it? Where are you throwing it?