Understanding Content-Type and Its Use in Spring MVC

This article explains the HTTP Content-Type header, outlines common media type values, and demonstrates how Spring MVC's RequestMapping annotation uses the consumes and produces attributes to filter requests and responses based on Content-Type, including practical code examples.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding Content-Type and Its Use in Spring MVC

In HTTP requests the Content-Type header specifies the media type of the request body, allowing servers to correctly interpret the data format.

Media types follow the pattern type/subtype(;parameter)?, where type is a primary category (e.g., text or image), subtype refines the format (e.g., html, png), and an optional parameter such as charset can provide additional details.

Common media types include:

text/html (HTML), text/plain (plain text), text/xml (XML), image/gif (GIF), image/jpeg (JPEG), image/png (PNG), and several application/* types such as application/json, application/xml, application/pdf, application/octet-stream, application/x-www-form-urlencoded, and multipart/form-data for file uploads.

Spring MVC leverages these types through the @RequestMapping annotation. The annotation can declare consumes to restrict which request Content‑Types are accepted and produces to specify the response Content‑Type, while other attributes like value, method, params, and headers further refine request handling.

Example of the @RequestMapping definition:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";
    @AliasFor("path") String[] value() default {};
    @AliasFor("value") String[] path() default {};
    RequestMethod[] method() default {};
    String[] params() default {};
    String[] headers() default {};
    String[] consumes() default {};
    String[] produces() default {};
}

Key attributes: value: the URL pattern (e.g., /user/details/info); method: HTTP method such as GET, POST; consumes: required request Content‑Type (e.g., application/json); produces: response Content‑Type when the request’s Accept header matches; params and headers: additional constraints.

Typical usage in a controller:

@Controller
@RequestMapping(value = "/addUser", method = RequestMethod.POST,
    consumes = "application/json", produces = "application/json")
@ResponseBody
public String addUser(@RequestBody User user) {
    // TODO: implement logic
    Map<String, String> result = new HashMap<>();
    result.put("errCode", "200");
    result.put("msg", "success");
    return result;
}

This method only processes requests whose Content-Type is application/json and returns a JSON response when the client’s Accept header includes application/json. The consumes and produces attributes thus act as filters based on Content‑Type, while headers can also be used for similar checks.

Developers can inspect actual request headers using browser developer tools to see which Content‑Type values are sent by various endpoints.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendHTTPSpring MVCContent-TypeMedia Type
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.