I am having two Spring Rest service create-employee and create-staff like as shown below
create-employee
#RequestMapping(value="/create-employee", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<Void> createEmployee(final #RequestBody User user) {
try {
// employee createion logic
} catch (Exception exception) {
log.error("Exception in createEmployee:"+exception.getMessage());
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
}
create-staff
#RequestMapping(value="/create-staff", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<Void> createStaff(final #RequestBody User user) {
try {
// staff creation logic
} catch (Exception exception) {
log.error("Exception in createStaff:"+exception.getMessage());
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
}
For both the services I am using a dto named User like as shown below:
public class User {
#JsonProperty("employeeName")
private String name;
#JsonProperty("age")
private Integer age;
#JsonProperty("managerName")
private String headName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getHeadName() {
return headName;
}
public void setHeadName(String headName) {
this.headName = headName;
}
}
Now the issue is that for both create-employee and create-staff since I am using User.java as the request body. The posting json body looks like this
{
"employeeName" : "string",
"age" : "integer",
"managerName" : "string"
}
but actually what I want is that for create-staff service I would like to have the json body as below
{
"staffName" : "string",
"age" : "integer",
"managerName" : "string"
}
and create-staff service I would like to have the json body as below
{
"employeeName" : "string",
"age" : "integer",
"managerName" : "string"
}
But for both the services I need to use the same User.java dto but with different JsonProperty for the two services
Can anyone please hep me on this
Jackson also supports JsonAlias which might be helpful for you, just make sure you updated your jacskon mapper to version 2.9.1
public class User {
#JsonAlias({"employeeName","staffName"})
private String name;
#JsonProperty("age")
private Integer age;
#JsonProperty("managerName")
private String headName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getHeadName() {
return headName;
}
public void setHeadName(String headName) {
this.headName = headName;
}
}
Related
I am trying to use JSON inside a JSON request.
for example:
{
"name":"newdeeeepaajlf",
"category":"fsafaa",
"jsonData":{
"a":"value"
}
}
now when I am trying to get it in my DTO which has
private JSONObject jsonData;
it gets converted in a blank JSON
{}
I am stuck in this.
We can use map to convert the data
public class TestModel {
private String name;
private String category;
private Map<String, Object> jsonObj;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Map<String, Object> getJsonObj() {
return jsonObj;
}
public void setJsonObj(Map<String, Object> jsonObj) {
this.jsonObj = jsonObj;
}
}
and use above class from controller like below
#PostMapping("/test")
public boolean test(#RequestBody TestModel model) {
System.out.println(model.getCategory());
System.out.println(model.getName());
JSONObject jsonObj = new JSONObject(model.getJsonObj());
System.out.println(jsonObj);
return true;
}
For request
{
"category":"json",
"name":"name",
"jsonObj": {
"a": "value"
}
}
it will print
json
name
{a=value}
you have a error in json if you have json something like below.
{
"name": "newdeeeepaajlf",
"category": "fsafaa",
"jsonData": {
"a": "value"
}
}
you can use this as a class
public class Codebeautify {
private String name;
private String category;
JsonData jsonDataObject;
// Getter Methods
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public JsonData getJsonData() {
return jsonDataObject;
}
// Setter Methods
public void setName(String name) {
this.name = name;
}
public void setCategory(String category) {
this.category = category;
}
public void setJsonData(JsonData jsonDataObject) {
this.jsonDataObject = jsonDataObject;
}
}
public class JsonData {
private String a;
// Getter Methods
public String getA() {
return a;
}
// Setter Methods
public void setA(String a) {
this.a = a;
}
}
also json within json is working with in spring boot its a very common scenario. use ObjectMapper to map json with class.
I have an API method implemented in spring boot for Courses. It fetches the course by topic Id. The Course class is implemented as:
#Entity
public class Course {
#Id
private String id;
private String name;
private String description;
#ManyToOne
private Topic topic;
public Course() {
}
public Course(String id, String name, String description, String topicId) {
super();
this.id = id;
this.name = name;
this.description = description;
this.topic = new Topic(topicId, "", "");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setTopic(Topic t) {
this.topic = t;
}
}
And the API method is implemented as:
#RequestMapping(method=RequestMethod.GET, value="/topics/{topicId}/courses")
public RestMessage getAllCourses(#PathVariable String topicId) {
try {
List<Course> course = courseService.getAllCourses(topicId);
message = new RestMessage(course,StatusCodeEnum.OK);
return message;
} catch (Exception e) {
message = new RestMessage(e.getMessage(),StatusCodeEnum.INTERNAL_SERVER_ERROR);
message.setException(e);
return message;
}
}
The method implementation is simple, it tries to get all the courses based on the topic id and return it as a RestMessage Object. I'm using postman for the testing and in the response I am getting the list of Course but the Topic entity data is discarded.
The api response is as:
{
"data": [
{
"id": "java-streams",
"name": "Java Streams",
"description": "Java Stream learning"
}
],
"httpStatus": "OK",
"statusCode": 200,
"exception": null
}
And the RestMessage Class is defined as:
public class RestMessage {
private Object data;
private StatusCodeEnum httpStatus;
private int statusCode;
private Exception ex;
public RestMessage() {
}
public RestMessage(Object d, StatusCodeEnum c) {
data = d;
httpStatus = c;
statusCode = c.val();
}
public void setData(Object d) {
data =d;
}
public void setHttpStatus(StatusCodeEnum c) {
httpStatus = c;
}
public void setStatusCode(int c) {
statusCode = c;
}
public void setException(Exception e) {
ex = e;
}
public void setStatusCode(StatusCodeEnum c) {
httpStatus = c;
}
public Object getData() {
return data;
}
public StatusCodeEnum getHttpStatus() {
return httpStatus;
}
public Exception getException() {
return ex;
}
public int getStatusCode() {
return statusCode;
}
}
However, I have tried to debug the API endpoint and before returning the RestMessage object I have data in the required shape but after getting the json response the Topic object is truncated for all the courses.
The debug data image is attached:
I wonder what I am doing wrong in this case?
The field topic from Course doesn't have a getter, that's why is ignored by JSON serializer.
You can use Lombok annotation to automatically generate getters and setters.
Just add #Dataon class definition and you will have generated getters and setters, constructor without parameters. You will have everything for what you can said that is POJO class.
Check this link: Project lombok
I have a enum like as:
public enum Age {
THREE("3"),
FIVE("5");
private final String value;
Age(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
and a User class like:
public class User {
#NotNull
String name;
Age age;
public User() {
}
public User(#NotNull String name, Age age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Age getAge() {
return age;
}
public void setAge(Age age) {
this.age = age;
}
}
and a RestController like:
#ExceptionHandler(HttpMessageNotReadableException.class)
#ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<User> exceptionHandling(HttpMessageNotReadableException exception,
HandlerMethod handlerMethod, WebRequest webRequest) {
logger.error("error:" + exception.getLocalizedMessage());
EnumValidationException ex = (EnumValidationException) exception.getMostSpecificCause();
User user = new User();
user.setName(""); // I want set user's input
user.setAge(Age.FIVE);
return ResponseEntity.ok(user);
}
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<User> exceptionHandling2(MethodArgumentNotValidException exception) {
logger.error("error:" + exception.getLocalizedMessage());
User user = new User();
user.setName(""); // I want set user's input
user.setAge(Age.FIVE);
return ResponseEntity.ok(user);
}
#PostMapping("/user2")
public String setUser2(#Valid #RequestBody User user) {
return "ok";
}
I sed a JSON like:
{
"name":"Name",
"age":"11"
}
now how I handle its HttpMessageNotReadableException exceptions that return Name in name field in response?
should I change enum to static final String?
can I write customized Annotaion? how handle its getValue()?
Note: I use Hibernate.
If I understood your question, you want to print the value of Age, not the
name: in that case, you can try overriding toString() in Age:
#Override
public String toString() {
return value;
}
If you want to use getValue(), you should read the documentation of Spring Boot and especially its underlying JSON API:
https://github.com/FasterXML/jackson
https://github.com/FasterXML/jackson-annotations
https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations
I have followed this tutorial to build REST API using Spring boot. It taught alot. But What I am trying to do really got me stuck.
What I am trying to get is:
{
"marks":{
"id":"1",
"name":"test",
"remark":"passed",
"course": {
"id": "1",
"name": "Spring Boot",
"description": "Solves many problems",
"topic": {
"id": "1",
"name": "Java",
"description": "Powerful Programming Language"
}
}
But I get the error when I tried to add the marks- as :
{
"timestamp": 1515600105327,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.bind.MissingPathVariableException",
"message": "Missing URI template variable 'courseId' for method parameter of type String",
"path": "/topics/1/courses/1/marks"
}
My Marks Model is:
public class Marks {
#Id
private String id;
private String name;
private String remark;
#ManyToOne
private Course course;
#ManyToOne
private Topic topic;
public Marks() {
}
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
public Marks(String id, String name, String remark,String topicId, String courseId) {
this.id = id;
this.name = name;
this.remark = remark;
this.topic = new Topic(topicId, "","");
this.course = new Course(courseId, " ", " ", " ");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
And MarksController.java:
public class MarksController {
#RestController
public class MarksController {
#Autowired
private MarksService marksService;
#RequestMapping("/topics/{topicId}/courses/{id}/marks")
public List<Marks> getAllMarks(#PathVariable String courseId) {
return marksService.getAllMarks(courseId);
}
#RequestMapping(method=RequestMethod.POST, value="/topics/{topicId}/courses{courseId}/marks")
public void addMarks(#RequestBody Marks marks,#PathVariable String topicId ,#PathVariable String courseId) {
marks.setTopic(new Topic(topicId, "", ""));
marks.setCourse(new Course(courseId, "", "", ""));
marksService.addMarks(marks);
}
}
And MarksService.java:
public class MarksService {
#Service
public class MarksService {
#Autowired
private MarksRepository marksRepository;
public void addMarks(Marks marks) {
marksRepository.save(marks);
}
}
And MarksRepository.java:
public interface MarksRepository extends CrudRepository<Marks, String> {
public List<Marks> findByCourseId(String courseId);
public List<Marks> findByTopicId(String topicId);
}
Can anyone help me get the result as in the mentioned JSON.
For the POST method
This:
/topics/{topicId}/courses{courseId}/marks
Should be:
/topics/{topicId}/courses/{courseId}/marks
Note the additional / between courses and {courseId}
For the GET method
This:
/topics/{topicId}/courses/{id}/marks
Should be:
/topics/{topicId}/courses/{courseId}/marks
Note the use of courseId to agree with the parameter name in MarksController.getAllMarks.
Model
public class Organisation {
private String name;
public Organisation() { }
public Organisation(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
controller
#RequestMapping(method = RequestMethod.GET)
public List<Organisation> getAll() {
Organisation organisation = new Organisation("google");
List<Organisation> organisations = new ArrayList<>();
organisations.add(organisation);
return organisations;
}
This will give out response like this:
[
{
"name": "google"
}
]
What if we want something like this:
{
"data": [{
"type": "organisations"
"attributes": {
"name": "google"
}
]
}
So how to customize the json. I know that Spring MVC by default uses Jackson to convert models into JSON. Is there a way to customize it. I am trying to send response in JSONApi standard. Also can someone tell how to create links in responses
Create Classes as:
public class Object1 {
private List<Object2> data;
public Object1() {
}
public Object1(List<Object2> data) {
this.data = data;
}
//getters and setters
}
public class Object2 {
private String type;
private Object3 attributes;
public Object2() {
}
public Object2(String type, Object3 attributes) {
this.type = type;
this.attributes = attributes;
}
//getters and setters
}
public class Object3 {
private String name;
public Object3(String name) {
this.name = name;
}
public Object3() {
}
//getters and setters
}
Now your controller method shoul be like:
#RequestMapping(method = RequestMethod.GET)
public Object3 getAll() {
List<Object2> data = new ArrayList<>();
data.add(new Object2("organisations", new Object3("google")));
return new Object1(data);
}