You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Actuator 2.x @WriteOperation传对象参数的端点实现问询

Create a Web & JMX Accessible Endpoint with Object Parameter (Spring Boot 2.0.0.RELEASE)

Alright, let's build out this endpoint step by step. Based on your Spring Boot parent setup, here's how to get it done:

1. Update Your pom.xml Dependencies

First, make sure you have the necessary starters for actuator (which handles endpoints) and web (for HTTP access). Add these to your <dependencies> section:

<dependencies>
    <!-- Core Spring Boot Web support -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Actuator for endpoint management (supports Web + JMX) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

Your existing parent configuration is good to go (I've completed the encoding property for clarity):

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

2. Define Your Parameter Object

Create a simple POJO that represents the object your endpoint will receive. Make sure it has getters/setters (for JSON deserialization and JMX compatibility) and implements Serializable (required for JMX object transfer):

import java.io.Serializable;

public class RequestObject implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private String name;
    private int value;

    // Getters and Setters
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getValue() { return value; }
    public void setValue(int value) { this.value = value; }

    // Optional: toString for debugging
    @Override
    public String toString() {
        return "RequestObject{name='" + name + "', value=" + value + "}";
    }
}

3. Create the Dual-Access Endpoint

Use Spring Boot's @Endpoint annotation (this enables both Web and JMX access by default). Define an operation method that accepts your RequestObject as a parameter:

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.PostOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "customProcessor")
public class CustomProcessorEndpoint {

    // PostOperation maps to HTTP POST for Web access; works for JMX too
    @PostOperation
    public String processRequest(RequestObject request) {
        // Replace with your actual business logic
        String result = "Successfully processed request: " + request.toString();
        System.out.println(result);
        return result;
    }
}
  • @Endpoint(id = "customProcessor"): Sets the unique ID for your endpoint (used in both the Web URL and JMX MBean name).
  • @PostOperation: For Web access, this maps to a POST request (ideal for sending JSON payloads). For JMX, this will appear as an executable operation on the endpoint's MBean.

4. Configure Endpoint Exposure

Update your application.properties (or application.yml) to expose the endpoint via both Web and JMX:

# Expose your custom endpoint via Web
management.endpoints.web.exposure.include=customProcessor
# Expose all endpoints via JMX (or specify your custom one explicitly)
management.endpoints.jmx.exposure.include=*
# Optional: Set a base path for Web endpoints (default is /actuator)
management.endpoints.web.base-path=/actuator

5. Test the Endpoint

Web Access

Use curl or a tool like Postman to send a POST request with a JSON payload:

curl -X POST -H "Content-Type: application/json" -d '{"name":"test-request", "value":123}' http://localhost:8080/actuator/customProcessor

You should get a response like:

"Successfully processed request: RequestObject{name='test-request', value=123}"

JMX Access

  1. Start your Spring Boot application.
  2. Open jconsole (included with your JDK) and connect to your app's running process.
  3. Navigate to the org.springframework.boot domain → Endpoint → customProcessor.
  4. Find the processRequest operation, enter values for the request object's fields, and execute it. You'll see the result in JConsole, and your app's console will log the processed request.

Quick Notes

  • For JMX compatibility, always make your parameter object Serializable (we added this in step 2) to avoid serialization errors.
  • If you need to use GET instead of POST for Web access, you can use @ReadOperation, but keep in mind sending complex objects via GET isn't standard—you'd need to use request parameters instead of a JSON body.

内容的提问来源于stack exchange,提问作者Boris Daich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:29:17