mkyong

mkyong

Software Engineer • Writer • Dad

20+ years hands-on, writing about Java, Spring, and AI. Every code example here is tested.

1,960 posts

Posts by mkyong

Problem Deploying Jersey REST service, hit following error message on Tomcat. SEVERE: Servlet /RESTfulExample threw load() exception com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. //… code omitted Here’s the web.xml <web-app …> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.mkyong.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> Solution Many reasons […]

Read more Jersey : The ResourceConfig instance does not contain any root resource classes

This tutorial show you how to create a RESTful Java client with RESTEasy client framework, to perform “GET” and “POST” requests to REST service that created in last “Jackson + JAX-RS” tutorial. 1. RESTEasy Client Framework RESTEasy client framework is included in RESTEasy core module, so, you just need to declares the “resteasy-jaxrs.jar” in your […]

Read more RESTful Java client with RESTEasy client framework

Apache HttpClient is a robust and complete solution Java library to perform HTTP operations, including RESTful service. In this tutorial, we show you how to create a RESTful Java client with Apache HttpClient, to perform a “GET” and “POST” request. Note The RESTful services from last “Jackson + JAX-RS” article will be reused. 1. Get […]

Read more RESTful Java client with Apache HttpClient

Problem XML development in Spring MVC, via oxm , hit “HierarchicalStreamReader” class not found exception? Caused by: java.lang.ClassNotFoundException: com.thoughtworks.xstream.io.HierarchicalStreamReader at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) … 49 more Solution The class “HierarchicalStreamReader” class belong to “xstream.jar“. If you are using Maven, declares following dependency in your pom.xml file. <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.3.1</version> </dependency> Note For […]

Read more ClassNotFoundException : com.thoughtworks.xstream.io.HierarchicalStreamReader

Problem Using Jackson as JSON provider in RESTEasy. <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.1.GA</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>2.2.1.GA</version> </dependency> With RESTEasy auto scanning enabled. <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> When starting up, it hits following errors and failed to start up any of the RESTEasy services. SEVERE: Exception sending context initialized event to listener instance of class […]

Read more Illegal to inject a message body into a singleton into public org.codehaus.jackson.jaxrs.JacksonJsonProvider

Problem Developing RESTEasy + JAXB provider to support XML, when return it back to client, it prompts following error message : org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: com.mkyong.rest.User of media type: application/xml at org.jboss.resteasy.core.ServerResponse.writeTo(ServerResponse.java:216) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:500) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) //… Solution To use JAXB in RESTEasy, you need to include “resteasy-jaxb-provider.jar“. <repositories> […]

Read more RESTEasy – Could not find MessageBodyWriter for response object of type:xx of media type: application/xml

Not many complete file upload example in JAX-RS, especially RESTEasy. Here, we show you two complete RESTEasy examples to handle file upload from HTML form. Normal way to handle uploaded file via MultipartFormDataInput Map uploaded file to a POJO class via @MultipartForm 1. RESTEasy Multipart Dependency In RESTEasy, you need “resteasy-multipart-provider.jar” to handle multipart file […]

Read more File upload example in RESTEasy

In JAX-RS, for excel file, annotate the method with @Produces(“application/vnd.ms-excel”) : Put @Produces(“application/vnd.ms-excel”) on service method. Set “Content-Disposition” in Response header to prompt a download box. 1. Download Excel file in JAX-RS Full example to download an excel file from JAX-RS. import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/excel") public […]

Read more Download excel file from JAX-RS

Note Tested with Eclipse 3.6, Maven 3 and RESTEasy 2.2.1.GA Question Developing file upload feature with RESTEasy, see following RESTEasy multipart file upload example : public class FileUploadForm { private byte[] data; @FormParam("file") public void setData(byte[] data) { this.data = data; } //…code omitted } @Path("/file") public class UploadFileService { @POST @Path("/upload") @Consumes("multipart/form-data") public Response […]

Read more RESTEasy – Could not find message body reader for type: multipart/form-data

Question Developing REST service, with file upload function with resteasy, after added resteasy multipart dependency, following strange error message prompt during the application start up? P.S Using resteasy-jaxrs and resteasy-multipart version 2.2.1.GA. SEVERE: Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap java.lang.RuntimeException: Unable to scan WEB-INF for JAX-RS annotations, you must manually […]

Read more RESTEasy Unable to scan WEB-INF for JAX-RS annotations ?

In JAX-RS, for user to download an image file, annotate the method with @Produces(“image/image-type”) : Put @Produces(“image/png”) on service method, for “png” image. Set “Content-Disposition” in Response header to prompt a download box. Note For other image types, refer to this list of the image types 1. Download Image in JAX-RS Full example to download […]

Read more Download image file from JAX-RS

Problem In Jersey development, hit following error message on Tomcat. SEVERE: Servlet /RESTfulExample threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) //… Here’s the Maven pom.xml <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.8</version> </dependency> Solution The “com.sun.jersey.spi.container.servlet.ServletContainer” is included in “jersey-server.jar“, not “jersey-core.jar“. Actually, to develop REST service with Jersey, you just need to include “jersey-server.jar“, it […]

Read more ClassNotFoundException : com.sun.jersey.spi.container.servlet.ServletContainer

In this tutorial, we show you two ways to get HTTP request header in JAX-RS : Inject directly with @HeaderParam Pragmatically via @Context Note Refer to this wiki page for list of the HTTP header fields. 1. @HeaderParam Example In this example, it gets the browser “user-agent” from request header. import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import […]

Read more Get HTTP header in JAX-RS

In JAX-RS, you can use @FormParam annotation to bind HTML form parameters value to a Java method. The following example show you how to do it : 1. HTML Form See a simple HTML form with “post” method. <html> <body> <h1>JAX-RS @FormQuery Testing</h1> <form action="rest/user/add" method="post"> <p> Name : <input type="text" name="name" /> </p> <p> […]

Read more JAX-RS @FormParam example

In JAX-RS, for pdf file, annotate the method with @Produces(“application/pdf”) : Put @Produces(“application/pdf”) on service method. Set “Content-Disposition” in Response header to prompt a download box. 1. Download Pdf file in JAX-RS Full example to download a pdf file from JAX-RS. import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/pdf") public […]

Read more Download pdf file from JAX-RS

In JAX-RS, you can use @QueryParam annotation to inject URI query parameter into Java method. for example, /users/query?url=mkyong.com In above URI pattern, query parameter is “url=mkyong.com“, and you can get the url value with @QueryParam(“url”). 1. @QueryParam example See a full example of using @QueryParam in JAX-RS. import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; […]

Read more JAX-RS @QueryParam example

Question Working REST + file upload, using resteasy 2.2.1.GA and Eclipse 3.6. And the reseteasy multipart dependency is declared in maven pom.xml file. In compile mode, class “MultipartInput” is able to compile, but Eclipse prompts following error message during deployment or debugging mode? java.lang.NoClassDefFoundError: org/jboss/resteasy/plugins/providers/multipart/MultipartInput at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2427) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) Caused by: java.lang.ClassNotFoundException: […]

Read more ClassNotFoundException : org.jboss.resteasy.plugins.providers.multipart.MultipartInput

Matrix parameters are a set of “name=value” in URI path, for example, /books/2011;author=mkyong In above URI, the matrix parameter is “author=mkyong“, separate by a semi colon “;“. 1. @MatrixParam example See a full example of using @MatrixParam in JAX-RS. import javax.ws.rs.GET; import javax.ws.rs.MatrixParam; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/books") public class BookService { @GET […]

Read more JAX-RS @MatrixParam example

In JAX-RS, you can use @PathParem to inject the value of URI parameter that defined in @Path expression, into Java method. 1. @PathParam – Single Parameter A simple and normal way to use @PathParam. import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/users") public class UserRestService { @GET @Path("{id}") public Response getUserById(@PathParam("id") String id) { […]

Read more JAX-RS @PathParam example

In JAX-RS, you can use @Path to bind URI pattern to a Java method. See following examples to show you how it works. 1. Normal URI Matching See normal URI matching with @Path annotation. import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response; @Path("/users") public class UserRestService { @GET public Response getUser() { return Response.status(200).entity("getUser is called").build(); } […]

Read more JAX-RS @Path URI matching example

There are many free Java eBooks, but most are outdated or not accurate. Here’s the best 5 free Java eBooks in my collection, they are well-known, complete, updated and detailed coverage of using Java programming language. Best for Java beginners and might also good reference for experienced programmers. P.S The order is based on my […]

Read more Top 5 free Java ebooks

Problem With Spring OXM + Castor binding, the Castor library is added, but still hit the following error message? Exception in thread "main" java.lang.RuntimeException: Could not instantiate serializer org.apache.xml.serialize.XMLSerializer: java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSerializer at org.exolab.castor.xml.XercesSerializer.<init>(XercesSerializer.java:50) //… Castor dependency in Maven. <dependency> <groupId>org.codehaus.castor</groupId> <artifactId>castor</artifactId> <version>1.2</version> </dependency> Solution If not mistake, Castor need Xerces to work, so, you need […]

Read more ClassNotFoundException: org.apache.xml.serialize.XMLSerializer

Problem In Spring OXM (object XML mapping), when converting an object to XML file, it hits following error message : Caused by: java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLException at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) … 29 more Is castor data binding framework included in Spring oxm? Solution The castor is […]

Read more ClassNotFoundException : org.exolab.castor.xml.XMLException

Normally, you will split a large Spring XML bean files into multiple small files, group by module or category, to make things more maintainable and modular. For example, <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="config/customer.xml"/> <import resource="config/scheduler.xml"/> </beans> In Spring3 JavaConfig, the equivalent functionality is @Import. package com.mkyong.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({ CustomerConfig.class, […]

Read more Spring 3 JavaConfig @Import example

Problem Using Spring3 @Configuration to create an application configuration file like below : import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean //… } However, when run it, it hits following error message : org.springframework.context.support.AbstractApplicationContext prepareRefresh //… Exception in thread "main" java.lang.IllegalStateException: CGLIB is required to process @Configuration classes. Either add CGLIB to the classpath or […]

Read more CGLIB is required to process @Configuration classes

In Spring EL, you can reference a bean, and nested properties using a ‘dot (.)‘ symbol. For example, “bean.property_name“. public class Customer { @Value("#{addressBean.country}") private String country; In above code snippet, it inject the value of “country” property from “addressBean” bean into current “customer” class, “country” property. Spring EL in Annotation See following example, show […]

Read more Spring EL bean reference example