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

使用Jersey发起POST请求时遭遇Error 415错误求助

Hey there! Let’s break down your POST request issue—since GET works perfectly, the problem’s almost definitely tied to how your app handles JSON payloads for POST, especially with that Gson/Jackson mix you mentioned. Here’s how to fix it step by step:

First: Fix the Gson vs. Jackson Conflict

You said you originally used Gson for JSON conversion, then added Jackson registration by extending Application to support JSON. That’s almost certainly the root cause—JAX-RS frameworks (like Jersey) prioritize registered JSON providers, so your app is trying to use Jackson to parse payloads while your code is still relying on Gson, causing a mismatch.

You have two clean options here—pick one to standardize your JSON handling:

Option 1: Stick with Gson (Remove Jackson)

If you prefer Gson, ditch the Jackson registration and set up Gson as your JAX-RS provider instead:

  1. Delete any Jackson-related registration code (like register(JacksonFeature.class) or custom ObjectMapper setup) from your Application subclass.

  2. Register Gson’s JAX-RS provider in your app class:

    @ApplicationPath("/")
    public class YourApp extends Application {
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> classes = new HashSet<>();
            // Add your service class here
            classes.add(YourService.class);
            // Register Gson's provider to handle JSON
            classes.add(GsonProvider.class);
            return classes;
        }
    }
    

    Make sure you have the gson-jaxrs dependency in your build (Maven/Gradle) to get GsonProvider.

  3. Keep using Gson in your service code as you did before—no need to switch to Jackson.

Option 2: Switch Fully to Jackson (Ditch Gson)

If you want to use Jackson (the more common JAX-RS default), replace all Gson code with Jackson’s ObjectMapper:

  1. Keep your existing Jackson registration (like JacksonFeature) in your Application subclass.
  2. Swap out Gson calls with Jackson:
    Replace this:
    Gson gson = new Gson();
    YourPojo payload = gson.fromJson(requestBody, YourPojo.class);
    
    With this:
    ObjectMapper mapper = new ObjectMapper();
    YourPojo payload = mapper.readValue(requestBody, YourPojo.class);
    
  3. Use Jackson’s annotations (like @JsonProperty, @JsonIgnore) on your POJOs if you need to customize serialization/deserialization.
Next: Verify the POST Request’s Content-Type Header

GET requests don’t send a payload, so they skip JSON parsing—but POST requests require the correct Content-Type header. Check the request you captured with nc to make sure it includes:

Content-Type: application/json
If it’s missing, either update your client to add this header, or (less ideally) add @Consumes(MediaType.TEXT_PLAIN) to your service method if the client is sending JSON as plain text.

Then: Double-Check Your Service Method Annotations

Make sure your POST method has the right @Consumes and @Produces annotations to match the request/response format. For example:

@POST
@Path("/your-endpoint")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response handlePostRequest(YourPojo requestPayload) {
    // Your business logic here
    return Response.ok().build();
}

If you’re manually parsing a String payload with Gson/Jackson, ensure @Consumes matches the client’s Content-Type (e.g., application/json even if you’re parsing the string yourself).

One Last Thing: Debug the Exact Error

If you’re still stuck, grab the full error stack trace from your server logs—this will tell you exactly if it’s a JSON parsing error (like invalid JSON syntax) or a provider conflict. You can also cross-check the request body you captured with nc to make sure it’s valid JSON (no missing commas, mismatched brackets, etc.).


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:35:48