I've been trying to start and stop Jetty automatically in mvn clean install phase for my unit tests. The annoying thing is when I deploy my war file in JBoss all the tests are working fine, however, when I try to use maven-jetty-plugin I have a Connection refuse: connect error. I'm sure about that Jetty starts but doesnt recognize the URLs so that it can't establish the connection.
pom.xml
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.13.v20130916</version>
<configuration>
<webApp>
<contextPath>/</contextPath>
</webApp>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
default web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>
and my other classes
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.buraktas.resource.DefaultBookResource;
#ApplicationPath("/services")
public class BaseApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public BaseApplication() {
singletons.add(new DefaultBookResource());
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
entity class
public class BookEntity {
private int id;
private String title;
private String author;
private double price;
... setters and getters
}
resource interface
#Path("/book-service")
public interface BookResource {
#POST
#Path("/create")
#Consumes("application/json")
#Produces("application/json")
public BookEntity createBook(BookEntity bookEntity);
}
and the implementation of it
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.buraktas.entity.BookEntity;
public class DefaultBookResource implements BookResource {
private Map<Integer, BookEntity> list = new HashMap<Integer, BookEntity>();
private AtomicInteger idCounter = new AtomicInteger();
#Override
public BookEntity createBook(BookEntity bookEntity) {
bookEntity.setId(idCounter.getAndIncrement());
list.put(bookEntity.getId(), bookEntity);
return bookEntity;
}
}
finally test class
public class TestClass {
private static final String POST_INPUT = "{\"title\" : \"LOTR\",\"author\" : \"J.K.Rowling\",\"price\" : 12.99}";
private static Client client;
#Before
public void setUp() {
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
client = Client.create(clientConfig);
}
#After
public void close() {
client.destroy();
}
#Test
public void testCreateSuccess() throws Exception {
WebResource webResourcePost = client.resource("http://localhost:8080/jersey-rest-client/services/book-service/create");
ClientResponse response = webResourcePost.accept("application/json").type("application/json").post(ClientResponse.class, POST_INPUT);
BookEntity responseEntity = response.getEntity(BookEntity.class);
assertEquals(200, response.getStatus());
assertEquals("LOTR", responseEntity.getTitle());
assertEquals("J.K.Rowling", responseEntity.getAuthor());
assertEquals(12.99, responseEntity.getPrice(), 0.0001);
}
}
Also you can find the whole implementation from here
https://github.com/flexelem/jersey-client-example
I will appreciate for every response. Thanks anyway
Related
Below is my code, I am using command line tool, jax-rs, rest apis and java
I get this error: "SEVERE: MessageBodyWriter not found for media type = text/plain, type=class java.util.ArrayList, genericType=class java.util.ArrayList", when I go to localhost:8888/quotes after the command "mvn exec:java"
What am I doing wrong? I have a main class, Quote class, QuoteResource class, QuoteService class, quoteTest class, and pom.xml with the following code:
package edu.depaul.se457.bisbell;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
/**
* Root resource (exposed at "quotes" path)
*/
#Path("quotes")
public class QuoteResource {
private QuoteService quoteService = new QuoteService();
#GET
#Produces(MediaType.TEXT_PLAIN)
public Response getAllQuotes() {
List<Quote> quotes = quoteService.getAllQuotes();
if (!quotes.isEmpty()){
return Response.ok(quotes).build();
}else{
return Response.status(Response.Status.NOT_FOUND).build();
}
}
#Path("/{id}")
#GET
#Produces(MediaType.TEXT_PLAIN)
//public int getQuoteByID(#PathParam("id") int id){
public Response getQuoteById(#PathParam("id") int id) throws NotFoundException{
Quote quote = quoteService.fetchBy(id);
if (quote.id.equals(null)) {
return Response.status(Response.Status.NOT_FOUND).build();
}else{
return Response.ok(quote).build();
}
}
#POST
#Produces(MediaType.TEXT_PLAIN)
#Consumes(MediaType.TEXT_PLAIN)
public Response createQuote(Quote quote){
boolean result = quoteService.create(quote);
if (result){
return Response.ok().status(Response.Status.CREATED).build();
}else{
return Response.notModified().build();
}
}
#PUT
#Path("/{id}")
#Consumes(MediaType.TEXT_PLAIN)
public Response updateQuote(#PathParam("id") int id, Quote quote) {
boolean result = quoteService.update(quote);
if (result) {
return Response.ok().status(Response.Status.NO_CONTENT).build();
} else {
return Response.notModified().build();
}
}
#Path("/{id}")
#DELETE
#Produces(MediaType.TEXT_PLAIN)
public Response deleteQuote(#PathParam("id") int id) {
boolean result = quoteService.delete(id);
if (result) {
return Response.ok().status(Response.Status.NO_CONTENT).build();
} else {
return Response.notModified().build();
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.depaul.se457.bisbell;
/**
*
* #author bkisb
*/
public class Quote {
Integer id;
String quote;
public Quote() {
}
public Quote(String quote, int id){
super();
this.quote = quote;
this.id = id;
}
public String getQuote(){
return quote;
}
public void setQuote(String quote){
this.quote = quote;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
#Override
public String toString() {
return "Quote [id=" + id + "]";
}
boolean add(Quote quote) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
package edu.depaul.se457.bisbell;
import jakarta.ws.rs.NotFoundException;
import java.util.ArrayList;
import java.util.List;
/**
* Root resource (exposed at "quote" path)
*/
public class QuoteService {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* #return String that will be returned as a text/plain response.
*/
List<Quote> quotes = new ArrayList<>();
public List<Quote> getAllQuotes() {
quotes.add(new Quote("Some Quote 1", 1));
quotes.add(new Quote("Some Quote 2", 2));
quotes.add(new Quote("Some Quote 3", 3));
quotes.add(new Quote("Some Quote 4", 4));
quotes.add(new Quote("Some Quote 5", 5));
return quotes;
}
//public int getQuoteByID(#PathParam("id") int id){
public Quote fetchBy(int id) throws NotFoundException{
for (Quote quote: getAllQuotes()) {
if (id == quote.getId()){
return quote;
}
else{
throw new NotFoundException("Resource not found with ID ::" + id);
}
}
return null;
}
public boolean create(Quote quote){
return quote.add(quote);
}
public boolean update(Quote quote){
for (Quote updateQuote: quotes) {
if (quote.getId() == (updateQuote.getId())){
quotes.remove(updateQuote);
quotes.add(quote);
return true;
}
}
return false;
}
public boolean delete(int id) throws NotFoundException {
for (Quote quote: quotes) {
if (quote.getId() == (id)) {
quotes.remove(quote);
return true;
}
}
return false;
}
}
package edu.depaul.se457.bisbell;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import org.glassfish.grizzly.http.server.HttpServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class quoteTest {
private HttpServer server;
private WebTarget target;
#Before
public void setUp() throws Exception {
// start the server
server = Main.startServer();
// create the client
Client c = ClientBuilder.newClient();
// uncomment the following line if you want to enable
// support for JSON in the client (you also have to uncomment
// dependency on jersey-media-json module in pom.xml and Main.startServer())
// --
// c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());
target = c.target(Main.BASE_URI);
}
#After
public void tearDown() throws Exception {
server.stop();
}
/**
* Test to see that the message "Got it!" is sent in the response.
*/
#Test
public void testGetIt() {
String responseMsg = target.path("quote").request().get(String.class);
assertEquals("Got It!", responseMsg);
}
}
package edu.depaul.se457.bisbell;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;
/**
* Main class.
*
*/
public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8888/";
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* #return Grizzly HTTP server.
*/
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in edu.depaul.se457.bisbell package
final ResourceConfig rc = new ResourceConfig().packages("edu.depaul.se457.bisbell");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
/**
* Main method.
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with endpoints available at "
+ "%s%nHit Ctrl-C to stop it...", BASE_URI));
System.in.read();
server.stop();
}
}
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.depaul.se457.bisbell</groupId>
<artifactId>hw2</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>hw2</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>edu.depaul.se457.bisbell.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>3.0.4</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I am using Spring Boot 2 to create web application and running the application using CommandLineRunner to connect PostgreSql database
1 . "LinkRepository" Interface:
package com.example.demo;
import org.springframework.data.repository.CrudRepository;
import com.example.entity.Link;
public interface LinkRepository extends CrudRepository<Link, Long> {
}
2. 'Link' Entity :
package com.example.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "link")
public class Link {
#Id
#GeneratedValue
#Column(name = "id")
private Long id;
#Column(name = "NAME")
private String name;
#Column(name = "url", unique = true)
private String url;
public Link(String name, String url) {
this.name = name;
this.url = url;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
3. Demo Application Config:
package com.example.demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.example.entity.Link;
#SpringBootApplication(scanBasePackages = { "com.example" })
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Bean
public CommandLineRunner demo(LinkRepository repository) {
// TODO Auto-generated method stub
return (args) -> {
repository.save(new Link("test", "link"));
for (Link linkrepo : repository.findAll()) {
System.out.println(linkrepo.getName());
}
};
}
}
4. 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.1.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5. Application.properties :
spring.datasource.url=jdbc:postgresql://localhost:5432/TestDb
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto = create
spring.h2.console.enabled=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
I am getting following error :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo' defined in com.example.demo.DemoApplication: Unsatisfied dependency expressed through method 'demo' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'linkRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.entity.Link
If you want to use CommandLineRunner, then that should be something like this:
`#SpringBootApplication(scanBasePackages = { "com.example" })
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
return (args) -> {
repository.save(new Link("test", "link"));
for (Link linkrepo : repository.findAll()) {
System.out.println(linkrepo.getName());
}
};
}
I've just prefer doing that like this:
1) Create a new class called for example DemoBootstrap
2) And it should be soemthing like this
#Component
public class DemoBootstrap implements ApplicationListener<ContextRefreshedEvent> {
private final LinkRepository categoryRepository;
public DemoBootstrap(LinkRepository linkRepository) {
this.linkRepository = linkRepository;
}
#Override
#Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
// Here add all links that should be saved
// for example
linkRepository.save(new Link("foo", "bar"));
linkRepository.save(new Link("foo2", "bar2"));
// etc
}
Add #EntityScan("com.example.entity") on DemoApplication class or move DemoApplication to 'com.example' package and then spring will scan all sub-packages.
Hi I am trying to use Camel-SOAP component to unmarshal a soap message, sent using MQ.
But I can't figure out how to use ServiceInterfaceStrategy required by and SoapJaxbDataFormat
I'm using maven-jaxb2-plugin to generated my JAXB beans, using the provieded wsdl, xsd's.
What class should I use ?
And how do I generate it with maven-jaxb2-plugin ?
SoapJaxbDataFormat soap = new
SoapJaxbDataFormat("xx.xxx.service._201x._01._01.notification", new
ServiceInterfaceStrategy(WHAT_CLASS_TO_USE.class, false));
And how do I generate it with maven-jaxb2-plugin ?
To generate your class files using the plugin, try the following configuration:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/test/resources/wsdl</schemaDirectory>
<generatePackage>org.tempuri.calculator.jaxb2</generatePackage>
<generateDirectory>src/test/java</generateDirectory>
<clearOutputDir>false</clearOutputDir>
<episode>false</episode>
<strict>true</strict>
<schemaIncludes>
<schemaInclude>*.wsdl</schemaInclude>
<schemaInclude>*.xsd</schemaInclude>
</schemaIncludes>
</configuration>
</execution>
</executions>
</plugin>
The maven command to generate the sources is: mvn generate-sources.
What class should I use ?
To use it in your routes, try the following:
protected SoapJaxbDataFormat createDataFormat() {
String jaxbPackage = Add.class.getPackage().getName();
ElementNameStrategy elStrat = new TypeNameStrategy();
SoapJaxbDataFormat answer = new SoapJaxbDataFormat(jaxbPackage, elStrat);
answer.setVersion("1.2");
return answer;
}
#Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
#Override
public void configure() throws Exception {
SoapJaxbDataFormat df = createDataFormat();
from("direct:start") //
.marshal(df) //
.to("mock:result");
}
};
}
The message should be a XML like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope" xmlns:ns2="http://tempuri.org/">
<Body>
<ns2:Add>
<ns2:intA>10</ns2:intA>
<ns2:intB>20</ns2:intB>
</ns2:Add>
</Body>
</Envelope>
The POJO counterpart (generated by the jaxb2 plugin):
package org.tempuri.calculator.jaxb2;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"intA",
"intB"
})
#XmlRootElement(name = "Add")
public class Add {
protected int intA;
protected int intB;
public int getIntA() {
return intA;
}
public void setIntA(int value) {
this.intA = value;
}
public int getIntB() {
return intB;
}
public void setIntB(int value) {
this.intB = value;
}
}
Don't forget to add the camel-soap dependency to your pom file:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-soap</artifactId>
</dependency>
The WSDL used by this example could be found here, based on this unit test.
Note : I am using PostgreSQL database
After the build and run table is not created in database
I do same as broadleaf documentation for create new entity:
1. **Created new interface** :
package com.mycompany.entity.ordereturn;
public interface OrderReturn {
Long getId();
void setId(Long id);
String getReason();
void setReason(String reason);
String getAddress();
void setAddress(String address);
}
Created new OrderReturnImpl class :
#Entity
#Table(name = "blc_order_return")
public class OrderReturnImpl implements OrderReturn{
#Id
#GeneratedValue(generator= "OrderReturnId")
#GenericGenerator(
name="OrderReturnId",
strategy="org.broadleafcommerce.common.persistence.IdOverrideTableGenerator",
parameters = {
#Parameter(name="segment_value", value="OrderReturnImpl"),
#Parameter(name="entity_name", value="com.mycompany.entity.ordereturn.OrderReturnImpl")
}
)
#Column(name = "OrderReturn_ID")
protected Long id;
#Column(name = "Reason")
protected String reason;
#Column(name = "Address")
protected String address;
#Override
public Long getId() {
// TODO Auto-generated method stub
return id;
}
#Override
public void setId(Long id) {
// TODO Auto-generated method stub
this.id = id;
}
#Override
public String getReason() {
// TODO Auto-generated method stub
return reason;
}
#Override
public void setReason(String reason) {
// TODO Auto-generated method stub
this.reason = reason;
}
#Override
public String getAddress() {
// TODO Auto-generated method stub
return address;
}
#Override
public void setAddress(String address) {
// TODO Auto-generated method stub
this.address = address;
}
}
Configure in persistence-core.xml file :
<persistence-unit name="blPU" transaction-type="RESOURCE_LOCAL">
// Added this line in name = "blPU"
<class>com.mycompany.controller.account.OrderReturnImpl</class>
<exclude-unlisted-classes/>
</persistence-unit>
Although you havent posted your full OrderReturnImpl.java file, you're probably missing the #Entity annotation (along with #Table annotation).
Please annotate your class accordingly.
OK i see a new edit and you now have the annotations. So the error still continues with these annotations?
if you want to create the database tables from the entities, please use
<property name="hibernate.hbm2ddl.auto">create</property>
One possible problem is with creating tables if you are running admin/site on standalone server. Try it on embedded tomcat or jetty.
configure:
In admin/resource/runtime-properties/development.properties
blPU.hibernate.hbm2ddl.auto=create
blCMSStorage.hibernate.hbm2ddl.auto=create
blSecurePU.hibernate.hbm2ddl.auto=create
In core/resource/runtime-properties/common-shared.properties
blPU.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
blSecurePU.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
blCMSStorage.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
blPU.hibernate.hbm2ddl.import_files_sql_extractor=org.broadleafcommerce.common.util.sql.importsql.DemoPostgresSingleLineSqlCommandExtractor
blSecurePU.hibernate.hbm2ddl.import_files_sql_extractor=org.broadleafcommerce.common.util.sql.importsql.DemoPostgresSingleLineSqlCommandExtractor
blCMSStorage.hibernate.hbm2ddl.import_files_sql_extractor=org.broadleafcommerce.common.util.sql.importsql.DemoPostgresSingleLineSqlCommandExtractor
database.properties populated with your data
Be sure to have:
PostgreSQL driver in [standaloneserver]/lib or as a library if you run on embedded servers
Invalidated IDE cache
Clean and install maven goals on all project (or on all three modules)
postgresql start database in pom's executions and postgresql dependency
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>postgres-start</id>
<configuration>
<target>
<echo message="Starting postgres database..."/>
<java fork="true" spawn="true" classname="org.postgresql.Driver"
classpathref="maven.plugin.classpath">
</java>
<echo message="Database started successfully"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1211</version>
</dependency>
</dependencies>
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?