使用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:
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:
Delete any Jackson-related registration code (like
register(JacksonFeature.class)or customObjectMappersetup) from yourApplicationsubclass.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-jaxrsdependency in your build (Maven/Gradle) to getGsonProvider.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:
- Keep your existing Jackson registration (like
JacksonFeature) in yourApplicationsubclass. - Swap out Gson calls with Jackson:
Replace this:
With this:Gson gson = new Gson(); YourPojo payload = gson.fromJson(requestBody, YourPojo.class);ObjectMapper mapper = new ObjectMapper(); YourPojo payload = mapper.readValue(requestBody, YourPojo.class); - Use Jackson’s annotations (like
@JsonProperty,@JsonIgnore) on your POJOs if you need to customize serialization/deserialization.
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.
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).
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

