Set DEFAULT_VIEW_INCLUSION in YAML file
How I can set properties to DEFAULT_VIEW_INCLUSION in yml file in Spring?
Now, I have this
spring:
jackson:
mapper:
DEFAULT_VIEW_INCLUSION: true
But doesn't work.
json spring jackson
add a comment |
How I can set properties to DEFAULT_VIEW_INCLUSION in yml file in Spring?
Now, I have this
spring:
jackson:
mapper:
DEFAULT_VIEW_INCLUSION: true
But doesn't work.
json spring jackson
add a comment |
How I can set properties to DEFAULT_VIEW_INCLUSION in yml file in Spring?
Now, I have this
spring:
jackson:
mapper:
DEFAULT_VIEW_INCLUSION: true
But doesn't work.
json spring jackson
How I can set properties to DEFAULT_VIEW_INCLUSION in yml file in Spring?
Now, I have this
spring:
jackson:
mapper:
DEFAULT_VIEW_INCLUSION: true
But doesn't work.
json spring jackson
json spring jackson
edited Jul 17 '17 at 9:18
Anthon
30.1k1793147
30.1k1793147
asked Jul 17 '17 at 7:06
user6216601
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Try with Java Config:
@Configuration
public class JacksonMapperConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
return mapper;
}
}
Thanks, man! :)
– user6216601
Jul 17 '17 at 12:05
add a comment |
If you're using Spring Boot, you can autowire the ObjectMapper:
@Configuration
public class JacksonConfig {
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void configureObjectMapper() {
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
}
}
add a comment |
This is a somewhat older question, but the questioner asked about how to do it in YAML in Spring Boot (using the "application.yml" file), not how to override the Mapper. Since overriding the Mapper causes you to lose all the other auto-configure benefits, let's assume the mode of overriding was important to the questioner. So, I'll answer the YAML question.
The questioner's problem is the syntax. This is how the Spring Boot documentation says you set it in YAML:
spring:
jackson:
default-property-inclusion: non_null
Note the indentation, it's important. "spring" is up against the left margin, "Jackson" is one tab-stop indented, and then the property is another tab-stop indented. There must be a space between the ":" and the "non_null" property value or the parser doesn't see the property name correctly.
If you have other Spring properties to set, this section of your "application.yml" YAML file might look something like this:
spring:
cloud:
config:
enabled: false
jackson:
default-property-inclusion: non_null
Cautionary Note
That said, I tried to use this setting in a project with Spring Boot Starter v2.0.3 as its Parent, to suppress Nulls from Spring HATEOAS' ResourceSupport class. It didn't suppress any nulls in the Response objects.
This is where the overriding suggestion can offer a clue as to why the setting isn't 'working'. In my case, because the project used Jersey with Spring Boot instead of Spring MVC, I discovered that, for some reason, the JerseyConfiguration was overriding Boot's auto-configured mapper, and it explicitly set the JsonInclude to ALWAYS - so no Spring Boot setting was going to solve it:
public class JerseyConfiguration extends ResourceConfig {
@Autowired
public JerseyConfiguration(ObjectMapper objectMapper) {
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
packages("<removed actual class packages>");
register(ResponseFilter.class);
register(new ObjectMapperContextResolver(objectMapper));
configureSwagger();
}
So, one answer said to override the Mapper to 'solve' the problem. But you might find that your problem is occurring because someone already overrode the Spring Boot Mapper and has set an explicit include policy already. That was the issue on my project and changing the policy in the Jersey Config fixed my issue.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45137920%2fset-default-view-inclusion-in-yaml-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try with Java Config:
@Configuration
public class JacksonMapperConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
return mapper;
}
}
Thanks, man! :)
– user6216601
Jul 17 '17 at 12:05
add a comment |
Try with Java Config:
@Configuration
public class JacksonMapperConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
return mapper;
}
}
Thanks, man! :)
– user6216601
Jul 17 '17 at 12:05
add a comment |
Try with Java Config:
@Configuration
public class JacksonMapperConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
return mapper;
}
}
Try with Java Config:
@Configuration
public class JacksonMapperConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
return mapper;
}
}
answered Jul 17 '17 at 7:15
codependentcodependent
7,2701060128
7,2701060128
Thanks, man! :)
– user6216601
Jul 17 '17 at 12:05
add a comment |
Thanks, man! :)
– user6216601
Jul 17 '17 at 12:05
Thanks, man! :)
– user6216601
Jul 17 '17 at 12:05
Thanks, man! :)
– user6216601
Jul 17 '17 at 12:05
add a comment |
If you're using Spring Boot, you can autowire the ObjectMapper:
@Configuration
public class JacksonConfig {
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void configureObjectMapper() {
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
}
}
add a comment |
If you're using Spring Boot, you can autowire the ObjectMapper:
@Configuration
public class JacksonConfig {
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void configureObjectMapper() {
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
}
}
add a comment |
If you're using Spring Boot, you can autowire the ObjectMapper:
@Configuration
public class JacksonConfig {
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void configureObjectMapper() {
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
}
}
If you're using Spring Boot, you can autowire the ObjectMapper:
@Configuration
public class JacksonConfig {
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void configureObjectMapper() {
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
}
}
answered Jul 17 '17 at 9:01
René WinklerRené Winkler
2,89421746
2,89421746
add a comment |
add a comment |
This is a somewhat older question, but the questioner asked about how to do it in YAML in Spring Boot (using the "application.yml" file), not how to override the Mapper. Since overriding the Mapper causes you to lose all the other auto-configure benefits, let's assume the mode of overriding was important to the questioner. So, I'll answer the YAML question.
The questioner's problem is the syntax. This is how the Spring Boot documentation says you set it in YAML:
spring:
jackson:
default-property-inclusion: non_null
Note the indentation, it's important. "spring" is up against the left margin, "Jackson" is one tab-stop indented, and then the property is another tab-stop indented. There must be a space between the ":" and the "non_null" property value or the parser doesn't see the property name correctly.
If you have other Spring properties to set, this section of your "application.yml" YAML file might look something like this:
spring:
cloud:
config:
enabled: false
jackson:
default-property-inclusion: non_null
Cautionary Note
That said, I tried to use this setting in a project with Spring Boot Starter v2.0.3 as its Parent, to suppress Nulls from Spring HATEOAS' ResourceSupport class. It didn't suppress any nulls in the Response objects.
This is where the overriding suggestion can offer a clue as to why the setting isn't 'working'. In my case, because the project used Jersey with Spring Boot instead of Spring MVC, I discovered that, for some reason, the JerseyConfiguration was overriding Boot's auto-configured mapper, and it explicitly set the JsonInclude to ALWAYS - so no Spring Boot setting was going to solve it:
public class JerseyConfiguration extends ResourceConfig {
@Autowired
public JerseyConfiguration(ObjectMapper objectMapper) {
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
packages("<removed actual class packages>");
register(ResponseFilter.class);
register(new ObjectMapperContextResolver(objectMapper));
configureSwagger();
}
So, one answer said to override the Mapper to 'solve' the problem. But you might find that your problem is occurring because someone already overrode the Spring Boot Mapper and has set an explicit include policy already. That was the issue on my project and changing the policy in the Jersey Config fixed my issue.
add a comment |
This is a somewhat older question, but the questioner asked about how to do it in YAML in Spring Boot (using the "application.yml" file), not how to override the Mapper. Since overriding the Mapper causes you to lose all the other auto-configure benefits, let's assume the mode of overriding was important to the questioner. So, I'll answer the YAML question.
The questioner's problem is the syntax. This is how the Spring Boot documentation says you set it in YAML:
spring:
jackson:
default-property-inclusion: non_null
Note the indentation, it's important. "spring" is up against the left margin, "Jackson" is one tab-stop indented, and then the property is another tab-stop indented. There must be a space between the ":" and the "non_null" property value or the parser doesn't see the property name correctly.
If you have other Spring properties to set, this section of your "application.yml" YAML file might look something like this:
spring:
cloud:
config:
enabled: false
jackson:
default-property-inclusion: non_null
Cautionary Note
That said, I tried to use this setting in a project with Spring Boot Starter v2.0.3 as its Parent, to suppress Nulls from Spring HATEOAS' ResourceSupport class. It didn't suppress any nulls in the Response objects.
This is where the overriding suggestion can offer a clue as to why the setting isn't 'working'. In my case, because the project used Jersey with Spring Boot instead of Spring MVC, I discovered that, for some reason, the JerseyConfiguration was overriding Boot's auto-configured mapper, and it explicitly set the JsonInclude to ALWAYS - so no Spring Boot setting was going to solve it:
public class JerseyConfiguration extends ResourceConfig {
@Autowired
public JerseyConfiguration(ObjectMapper objectMapper) {
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
packages("<removed actual class packages>");
register(ResponseFilter.class);
register(new ObjectMapperContextResolver(objectMapper));
configureSwagger();
}
So, one answer said to override the Mapper to 'solve' the problem. But you might find that your problem is occurring because someone already overrode the Spring Boot Mapper and has set an explicit include policy already. That was the issue on my project and changing the policy in the Jersey Config fixed my issue.
add a comment |
This is a somewhat older question, but the questioner asked about how to do it in YAML in Spring Boot (using the "application.yml" file), not how to override the Mapper. Since overriding the Mapper causes you to lose all the other auto-configure benefits, let's assume the mode of overriding was important to the questioner. So, I'll answer the YAML question.
The questioner's problem is the syntax. This is how the Spring Boot documentation says you set it in YAML:
spring:
jackson:
default-property-inclusion: non_null
Note the indentation, it's important. "spring" is up against the left margin, "Jackson" is one tab-stop indented, and then the property is another tab-stop indented. There must be a space between the ":" and the "non_null" property value or the parser doesn't see the property name correctly.
If you have other Spring properties to set, this section of your "application.yml" YAML file might look something like this:
spring:
cloud:
config:
enabled: false
jackson:
default-property-inclusion: non_null
Cautionary Note
That said, I tried to use this setting in a project with Spring Boot Starter v2.0.3 as its Parent, to suppress Nulls from Spring HATEOAS' ResourceSupport class. It didn't suppress any nulls in the Response objects.
This is where the overriding suggestion can offer a clue as to why the setting isn't 'working'. In my case, because the project used Jersey with Spring Boot instead of Spring MVC, I discovered that, for some reason, the JerseyConfiguration was overriding Boot's auto-configured mapper, and it explicitly set the JsonInclude to ALWAYS - so no Spring Boot setting was going to solve it:
public class JerseyConfiguration extends ResourceConfig {
@Autowired
public JerseyConfiguration(ObjectMapper objectMapper) {
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
packages("<removed actual class packages>");
register(ResponseFilter.class);
register(new ObjectMapperContextResolver(objectMapper));
configureSwagger();
}
So, one answer said to override the Mapper to 'solve' the problem. But you might find that your problem is occurring because someone already overrode the Spring Boot Mapper and has set an explicit include policy already. That was the issue on my project and changing the policy in the Jersey Config fixed my issue.
This is a somewhat older question, but the questioner asked about how to do it in YAML in Spring Boot (using the "application.yml" file), not how to override the Mapper. Since overriding the Mapper causes you to lose all the other auto-configure benefits, let's assume the mode of overriding was important to the questioner. So, I'll answer the YAML question.
The questioner's problem is the syntax. This is how the Spring Boot documentation says you set it in YAML:
spring:
jackson:
default-property-inclusion: non_null
Note the indentation, it's important. "spring" is up against the left margin, "Jackson" is one tab-stop indented, and then the property is another tab-stop indented. There must be a space between the ":" and the "non_null" property value or the parser doesn't see the property name correctly.
If you have other Spring properties to set, this section of your "application.yml" YAML file might look something like this:
spring:
cloud:
config:
enabled: false
jackson:
default-property-inclusion: non_null
Cautionary Note
That said, I tried to use this setting in a project with Spring Boot Starter v2.0.3 as its Parent, to suppress Nulls from Spring HATEOAS' ResourceSupport class. It didn't suppress any nulls in the Response objects.
This is where the overriding suggestion can offer a clue as to why the setting isn't 'working'. In my case, because the project used Jersey with Spring Boot instead of Spring MVC, I discovered that, for some reason, the JerseyConfiguration was overriding Boot's auto-configured mapper, and it explicitly set the JsonInclude to ALWAYS - so no Spring Boot setting was going to solve it:
public class JerseyConfiguration extends ResourceConfig {
@Autowired
public JerseyConfiguration(ObjectMapper objectMapper) {
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
packages("<removed actual class packages>");
register(ResponseFilter.class);
register(new ObjectMapperContextResolver(objectMapper));
configureSwagger();
}
So, one answer said to override the Mapper to 'solve' the problem. But you might find that your problem is occurring because someone already overrode the Spring Boot Mapper and has set an explicit include policy already. That was the issue on my project and changing the policy in the Jersey Config fixed my issue.
edited Nov 14 '18 at 16:04
answered Nov 14 '18 at 15:11
CelticPoetCelticPoet
19529
19529
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45137920%2fset-default-view-inclusion-in-yaml-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown