Reputation: 569
I am new to the Spring Boot framework. I have a model like below :
public class Process{
private String processId;
private ProcessStatus status;
private hostname;
private errorMessage;
}
Post calls from the controller:
@RequestMapping(value = "/process", method = RequestMethod.POST,
consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public HttpStatus updateProcess (@RequestBody Process process)
{
try {
processService.updateProcess(process);
return HttpStatus.OK;
} catch (Exception e)
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR,e,
e);
}
}
Get call for the process :
@RequestMapping(value = "/process", method = RequestMethod.GET,
produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public Process getProcess (@RequestParam("processId") String processId)
{
try {
return processService.getProcess(processId);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR,
e);
}
}
So post is being performed by component-A who is responsible to update the information about the process. Get is being done by component-B to get the existing process. Question: How can i hide hostname and errorMessage for the get and but not for the post. When I do @JsonIgnore off course it will ignore for both requests.
Upvotes: 2
Views: 2210
Reputation: 38635
You can use JsonView
feature. You can define few different levels:
public class Views {
public static class Public {
}
public static class Internal extends Public {
}
}
Now, you need to annotate your POJO
attributes:
public class Process{
@JsonView(Views.Public.class)
private String processId;
@JsonView(Views.Public.class)
private ProcessStatus status;
@JsonView(Views.Internal.class)
private String hostname;
@JsonView(Views.Internal.class)
private String errorMessage;
}
When you already have this you need to annotate API
methods. GET
:
@RequestMapping(value = "/process", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@JsonView(Views.Public.class)
public Process getProcess (@RequestParam("processId") String processId)
And for the POST
:
@RequestMapping(value = "/process", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@JsonView(Views.Internal.class)
public HttpStatus updateProcess (@RequestBody Process process)
Of course, you can pick better names for views. Above is just an example usage after reading linked article. I did not test it but you should get general idea.
Upvotes: 1
Reputation: 16775
Simplest way is to use another DTO(data transfer object) class which does not contain the fields which you don't want to send back. In your case this would mean to create a ProcesDTO
class, which would look like this:
public class ProcessDTO {
private String processId;
private ProcessStatus status;
private hostname;
}
Also, this implies to do the mapping between Process
and ProcessDTO
, which can be done by hand or can be done by using some kind of library, like org.modelmapper.
Assuming that Process
is some kind of entity class, it is not really recommended anyway to use them directly to send back JSON data.
Upvotes: 0