Java Format | MuleSoft Documentation
# Java Format
MIME type: `application/java`
ID: `java`
For the Java data format, DataWeave attempts to map any Java value to a DataWeave value, most often by matching the semantics of DataWeave and Java.
## Java Value Mapping
The following table maps Java classes to DataWeave types.
Java Class | DataWeave  
---|---  
`java.lang.String`  
`CharSequence`  
`java.sql.Clob`  
`java.io.Reader`  
`BigInteger`  
`BigDecimal`  
`AtomicInteger`  
`AtomicLong`  
`AtomicBoolean`  
`java.util.Collection`  
`java.lang.Iterable`  
`java.util.Iterator`  
`java.util.Map`  
`java.util.Optional` | `Null` or `Value`  
`java.util.OptionalInt` | `Null` or `Number`  
`java.util.OptionalDouble` | `Null` or `Number`  
`java.util.OptionalLong` | `Null` or `Number`  
`java.lang.InputStream`  
`java.nio.ByteBuffer`  
`java.lang.File`  
`java.time.Instant` | `LocalDateTime`  
`java.time.LocalDateTime` | `LocalDateTime`  
`java.sql.Timestamp` | `LocalDateTime`  
`java.sql.Date` | `LocalDateTime`  
`java.util.Date` | `LocalDateTime`  
`java.time.ZonedDateTime`  
`java.time.LocalTime` | `LocalTime`  
`java.time.OffsetTime`  
`java.time.LocalDate`  
`java.time.ZoneOffset`  
`java.util.Calendar`  
`java.util.XMLGregorianCalendar`  
If a Java class is not in present in the table, DataWeave treats it as a `JavaBean` and maps it as an `Object` type. In this case, DataWeave takes all properties from the Java getters.   
---  
## Metadata
DataWeave supports the `^class` metadata for the Java format.
All Java objects are associated with a class. DataWeave maintains this association by mapping the class to a Metadata property.
`payload.^class` returns the name of the class of the `payload`.
The Java writer uses the metadata property to discover the Java class to which the DataWeave value maps. That is, DataWeave uses the metadata property to determine which Java class is to be created from a given DataWeave value. For example, the expression `output application/java --- now() as DateTime {class: "java.util.Calendar"}` creates a `java.util.Calendar`.
## Enum Custom Type
The Java format supports the `Enum` custom type. To put a Java `enum` value into a `java.util.Map`, the DataWeave Java module defines a custom type called `Enum`. This custom type enables you to handle a given string as the name of a specified `enum` type. You must use the `class` metadata property when specifying the Java class name of the enum, for example: `{gender: "Female" as Enum {class: "org.mycompany.Gender"}}`
## Handling `java.io.InputStream` Instances
Using `java.io.InputStream` instances in DataWeave, whether running DataWeave functions, calling Java methods from DataWeave code, or writing them to an output, requires understanding how DataWeave manages `java.io.InputStream` instances.
The Java format makes a deep copy of the `java.io.InputStream` instance. As a result, DataWeave creates a new `java.io.InputStream` instance that can be reused later in your script while retaining the content of the original stream.
However, if the original `java.io.InputStream` doesn’t support repeatable streams, it may get consumed and become unusable.
To avoid unexpected behavior, always check whether your `java.io.InputStream` is repeatable before processing it multiple times.
## Examples
The following examples show uses of the Java format.
  * Example: Access Values of Java Properties
  * Example: Create a Customer Object
  * Example: Use a Generic


### Class Definitions
The Java examples use the following class definitions:
  * `Customer` (which extends `User`)


User class:
```
class User {
  private String name;
  private String lastName;
  public User(){
  }
  public User(String name, String lastName){
    this.name = name;
    this.lastName = lastName;
  }
  public void setName(String name){
    this.name = name;
  }
   public void setLastName(String lastName){
    this.lastName = lastName;
  }
  public String getName(){
    return name;
  }
  public String getLastName(){
    return lastName;
  }
}
```

The following class extends `User`.
Customer class:
```
import java.util.Calendar;
class Customer extends User {
  private Calendar expirationDate;
  private User salesRepr;
  public User(){
  }
  public User(String name, String lastName,Calendar expirationDate){
    super(name,lastName);
    this.expirationDate = expirationDate;
  }
  public void setSalesRepr(User salesRepr){
    this.salesRepr = salesRepr;
  }
  public User getSalesRepr(){
    return this.salesRepr;
  }
  public void setExpirationDate(Calendar expirationDate){
    this.expirationDate = expirationDate;
  }
  public Calendar getExpirationDate(){
    return this.expirationDate;
  }
}
```

### Example: Access Values of Java Properties
This example shows how to access the values of Java properties.
#### Input
The `User` values serves as the input payload to the DataWeave script.
```
new User("Leandro", "Shokida")
```

#### Source
The DataWeave scripts transforms the input value to JSON.
```
output application/json
---
{
  a: payload.name,
  b: payload.lastName
}
```

#### Output
The output is a JSON object that contains key-value pairs. The values are the `name` and `lastName` values from the input `User` object.
```
{
  "a": "Leandro",
  "b": "Shokida"
}
```

### Example: Create a Customer Object
This example shows how to create an instance of a `Customer` class. The script outputs the object in the JSON format and MIME type.
Notice that it is not necessary to specify the class of inner properties because their class is inferred from the parent class definition.
```
output application/json
---
{
  name: "Tomo",
  lastName: "Chibana",
  expirationDate: now(),
  salesRepr: {
    name: "Mariano",
    lastName: "de Achaval",
  }
} as Object {class: "Customer"}
```

### Example: Use a Generic
This example relies on generic support in the class name to create a `java.util.ArrayList` of `User` objects.
Note that you do not need to use a generic to specify the class in each instance. The class is taken from the generic in the list.
```
output application/json
---
[{
  name: "Tomo",
  lastName: "Chibana"
},
{
  name: "Ana",
  lastName: "Felissati"
},
{
  name: "Leandro",
  lastName: "Shokida"
}
] as Array {class: "java.util.ArrayList<User>"}
```

## Java Support
Starting with version 2.6, DataWeave supports Java 17. Java 17 adds restrictions in encapsulation and reflective access that affects the Java Format. Because DataWeave uses Java’s reflection API to read and write Java objects, ensure that the objects your application uses are Plain Old Java Objects (POJOs) containing:
  * Default constructor
  * Getters for all properties
  * Setters for all properties


## Examples of Java Support
The following examples run using Java 17 and language level 2.6.
  * Example: Create a Java Object Instance
  * Example: Access the Values of Java Properties


### Example: Create a Java Object Instance
This example shows how to create a Java object instance.
#### User Class
The Java example uses the following class:
```
public class User {
  private String name;
  private String lastName;
  public User(){
  }
  public String getName(){
    return name;
  }
  public String getLastName(){
    return lastName;
  }
}
```

#### DataWeave Script
```
{name: "Tomo", lastName: "Chibana" } as Object {class: "User"}
```

#### Output
The expected output is an instance of the user class. Because the user class definition doesn’t have the proper setters, the output is `IllegalAccessException`.
```
java.lang.IllegalAccessException - class
org.mule.weave.v2.module.pojo.reader.PropertyDefinition (in module org.mule.weave.module.java) cannot access a member of class User with modifiers "private
```

### Example: Access the Values of Java Properties
This example shows how to access the values of Java properties.
#### Item Class
The Java example uses the following class:
```
public class Item {
	private String name;
	private int quantity;
	public Item() {
	}
  public Item(String name, int quantity) {
		this.name = name;
		this.quantity = quantity;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
}
```

#### Input
```
new Item("Pencil", 5)
```

#### DataWeave Script
```
output application/json
---
{
  "name": "payload.name",
  "quantity": "payload.quantity"
}
```

#### Output
The expected output is a JSON object.
```
{
  "name": "Pencil",
  "quantity": "5"
}
```
#### DataWeave Script
```
{name: "Tomo", lastName: "Chibana" } as Object {class: "User"}
```

#### Output
The expected output is an instance of the user class. Because the user class definition doesn’t have the proper setters, the output is `IllegalAccessException`.
```
java.lang.IllegalAccessException - class
org.mule.weave.v2.module.pojo.reader.PropertyDefinition (in module org.mule.weave.module.java) cannot access a member of class User with modifiers "private
```

### Example: Access the Values of Java Properties
This example shows how to access the values of Java properties.
#### Item Class
The Java example uses the following class:
```
public class Item {
	private String name;
	private int quantity;
	public Item() {
	}
  public Item(String name, int quantity) {
		this.name = name;
		this.quantity = quantity;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
}
```

#### Input
```
new Item("Pencil", 5)
```

#### DataWeave Script
```
output application/json
---
{
  "name": "payload.name",
  "quantity": "payload.quantity"
}
```

#### Output
The expected output is a JSON object.
```
{
  "name": "Pencil",
  "quantity": "5"
}
```

Because the item class definition doesn’t have the proper getters, the output is `IllegalAccessException`.
```
java.lang.IllegalAccessException: class
org.mule.weave.v2.module.pojo.reader.PropertyDefinition (in module org.mule.weave.module.java) cannot access a member of class Item with modifiers "private"
```

### Troubleshooting Common Errors
To resolve conflicts, ensure that you:
  * Update your class definitions to follow the POJOs structure (recommended).
  * Use the compatibility flag `com.mulesoft.dw.java.disable_set_accessible=false` to allow accessible flag modification for reflection objects.
This approach is a workaround and works for customer domain classes only. Note that the usage of this compatibility flag might not affect future versions.


## Configuration Properties
DataWeave supports the following configuration properties for this format.
### Reader Properties
There are no reader properties for this format.
### Writer Properties
This format accepts properties that provide instructions for writing output data.
Parameter | Type | Default | Description  
---|---|---|---  
`duplicateKeyAsArray` |  Converts the values of duplicate keys in an object to a single array of values to the duplicated key. Valid values are `true` or `false`.  
`writeAttributes` |  Converts attributes of a key into child key-value pairs of that key. The attribute key name starts with @. Valid values are `true` or `false`.  
## Supported MIME Types
This format supports the following MIME types.