Of course! This is an excellent question that gets to the heart of interoperability between Java and PHP.

Let's break down Hessian, its protocol, and how you can use it to connect PHP and Java applications.
What is Hessian?
At its core, Hessian is a binary web service protocol designed for efficiency and simplicity. It's an alternative to XML-based protocols like SOAP.
Key Characteristics:
- Binary: Instead of verbose text-based formats like XML, Hessian uses a compact binary format. This makes it much faster to parse and results in smaller network payloads.
- Stateless: Like most web services, Hessian is inherently stateless. Each request is independent.
- Simple API: It's designed to be easy to use. You don't need to generate complex WSDL files or client stubs. You just point your client to a service URL.
- Language Agnostic: While it was created by Caucho (the company behind the Resin application server), libraries exist for many popular languages, including Java, PHP, Python, C++, C#, and Ruby. This makes it a great choice for cross-platform communication.
How Does Hessian Work? (The Protocol Flow)
The Hessian protocol defines a contract for how objects and data types are serialized into a binary stream and sent over HTTP.

- HTTP POST: The client sends an HTTP POST request to a specific URL on the server.
- Hessian Headers: The request includes special HTTP headers to identify it as a Hessian call:
Content-Type: application/x-hessianAccept: application/x-hessian
- Binary Payload: The body of the request is a binary stream. This stream contains:
- The name of the remote method to be called.
- The serialized arguments for that method.
- Server Processing: The Hessian server on the Java side receives the request, deserializes the arguments, calls the corresponding Java method with those arguments, and serializes the return value.
- Response: The server sends back an HTTP response with the
Content-Type: application/x-hessianand the binary stream containing the serialized result (or an exception if one occurred).
Hessian Data Type Mapping:
Hessian has a well-defined mapping for common types between languages. For example:
String(Java) <->string(PHP)int,long(Java) <->int(PHP)Map,Hashtable(Java) <->array(PHP, associative)List,Set,Collection(Java) <->array(PHP, indexed)Date(Java) <->DateTime(PHP)- Custom Java Beans are serialized by their fields and can be mapped to PHP objects or associative arrays.
Hessian in Java (The Server Side)
In Java, Hessian is typically implemented using a Servlet. You expose a plain Java object as a remote service by wrapping it in a HessianServlet.
Step 1: Add Dependencies
You'll need the Hessian library. If you're using Maven, add this to your pom.xml:

<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.63</version> <!-- Check for the latest version -->
</dependency>
Step 2: Create Your Service Interface and Implementation
This is a standard Java POJO (Plain Old Java Object).
// UserService.java (The contract)
package com.example.service;
public interface UserService {
String getGreeting(String name);
User findUserById(long id);
}
// User.java (A simple data object)
package com.example.service;
public class User {
private long id;
private String name;
private String email;
// Constructors, Getters, and Setters are essential!
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public long getId() { return id; }
public String getName() { return name; }
public String getEmail() { return email; }
}
// UserServiceImpl.java (The implementation)
package com.example.service;
public class UserServiceImpl implements UserService {
@Override
public String getGreeting(String name) {
return "Hello, " + name + " from Java Hessian Service!";
}
@Override
public User findUserById(long id) {
// In a real app, you'd fetch this from a database
if (id == 1) {
return new User(1, "Alice", "alice@example.com");
}
return null;
}
}
Step 3: Configure the Servlet in web.xml
In your Java web application's WEB-INF/web.xml file, you map a URL to your service implementation.
<web-app>
<servlet>
<servlet-name>UserHessianService</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<!-- The fully qualified name of your service implementation -->
<init-param>
<param-name>home-api</param-name>
<param-value>com.example.service.UserService</param-value>
</init-param>
<init-param>
<param-name>home-class</param-name>
<param-value>com.example.service.UserServiceImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>UserHessianService</servlet-name>
<url-pattern>/UserService</url-pattern>
</servlet-mapping>
</web-app>
Now, your Java service is available at http://your-java-app-domain:port/YourAppContext/UserService.
Hessian in PHP (The Client Side)
On the PHP side, you use a client library to connect to the Java service URL.
Step 1: Install the PHP Library
The most common library is phphessian. You can install it using Composer:
composer require hessianphp/hessian
Step 2: Create the PHP Client
This script will call the methods exposed by the Java UserService.
<?php
// require the autoloader
require 'vendor/autoload.php';
use HessianPHP\Client\HessianClient;
// The URL of the Java Hessian service
$serviceUrl = 'http://your-java-app-domain:port/YourAppContext/UserService';
try {
// 1. Create the Hessian client
// The second argument is the API class (interface) from the Java side.
// The client will use this for type hinting and method validation.
$hessianClient = new HessianClient($serviceUrl, 'com.example.service.UserService');
echo "--- Calling getGreeting ---\n";
// 2. Call a remote method as if it were a local one
$greeting = $hessianClient->getGreeting('Bob from PHP');
echo "Result from Java: " . $greeting . "\n\n";
echo "--- Calling findUserById ---\n";
// 3. Call a method that returns a complex object
$user = $hessianClient->findUserById(1);
if ($user) {
// The returned object is a PHP object with properties mapped from the Java bean
echo "Found User:\n";
echo " ID: " . $user->id . "\n";
echo " Name: " . $user->name . "\n";
echo " Email: " . $user->email . "\n";
} else {
echo "User not found.\n";
}
} catch (Exception $e) {
// Hessian-specific exceptions will be thrown on errors
echo "Error calling Hessian service: " . $e->getMessage() . "\n";
echo "Trace: " . $e->getTraceAsString() . "\n";
}
?>
Hessian vs. Alternatives (SOAP, REST)
| Feature | Hessian | SOAP (e.g., JAX-WS) | REST (with JSON) |
|---|---|---|---|
| Format | Binary | XML (verbose) | Text (JSON, XML, etc.) |
| Performance | Very High (small payload, fast parsing) | Low (large payload, slow parsing) | High (small JSON payload, fast parsing) |
| Complexity | Low (no WSDL, simple API) | High (WSDL, XSD, complex standards) | Medium (API design is key) |
| Tooling | Low (manual client setup) | Very High (code generation, IDE support) | High (many client libraries) |
| Standards | Proprietary (Caucho) | W3C Standard | Architectural Style (not a standard) |
| Best For | High-performance, internal system-to-system communication. | Enterprise-grade, highly standardized, WS-* security. | Public APIs, web/mobile apps, general-purpose. |
Summary: When to Use Hessian for PHP/Java
-
Use Hessian when:
- Performance is a critical factor (low latency, high throughput).
- You are connecting two internal systems where you control both ends.
- You want a simpler alternative to SOAP without the overhead of REST's stateless design concerns.
- The data being exchanged is complex and benefits from a binary format.
-
Avoid Hessian when:
- You need to create a public API that must be accessible by a wide variety of clients (REST/JSON is the universal standard).
- You require standardized security mechanisms like WS-Security.
- Your team is more comfortable with RESTful principles and tooling.
- The overhead of Hessian isn't justified for your simple use case.
