Related
Good morning I have a small query, I am doing a small web service rest with spring boot, the issue is that it is working fine and everything else, as I am doing as follows, receives a parameter and returns a response based on a Stored Procedue in the database:
But now I have changed the request, and it is including header and body, like the following:
{
"ValidateClient": {
"Header": {
"country": "VE",
"lang": "ES",
"entity": "TMVE",
"system": "76",
"subsystem": "APP",
"operation": "ValidateClient",
"timestamp": "2019-10-23T08:48:08.474Z",
"msgType": "REQUEST"
},
"Body": {
"validateClientRequest": {
"movil": "04141734272"
}
}
}
}
Which when executing it gives me an answer of not found the mobile, it is a default response when it cannot read the mobile parameter or it is sent empty
My Code
Main Class
package com.app.validate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ValidateClientApp {
public static void main(String[] args) {
SpringApplication.run(ValidateClientApp.class, args);
}
}
Controller
package com.app.validate.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.app.validate.dao.ValidateClientAppRepository;
import com.app.validate.entity.DriverBonificados;
import com.app.validate.entity.ResponseVo;
#RestController
public class ValidateClientAppController {
#Autowired
private ValidateClientAppRepository dao;
#PostMapping(value = "/ValidateClientApp",consumes = "application/json",produces="application/json")
public ResponseVo ValidateClient(#RequestBody DriverBonificados driver) {
//System.out.println(driver.getMovil());
return dao.validarClienteBonifiado(driver.getMovil());
}
}
Dao
package com.app.validate.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.app.validate.entity.DriverBonificados;
import com.app.validate.entity.ResponseVo;
#Repository
public interface ValidateClientAppRepository extends JpaRepository<DriverBonificados, Integer> {
#Query(nativeQuery = true,value = "call ValidacionClienteBonificado(:movil)")
ResponseVo validarClienteBonifiado(#Param("movil") String pMovil);
}
Entity
package com.app.validate.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="DriverBonificados")
public class DriverBonificados {
#Id
private int id;
private String movil;
private String contador;
private Date fecha_driver;
private Date fecha_alta;
private Date fecha_fin;
private Date codigo_transaccion;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMovil() {
return movil;
}
public void setMovil(String movil) {
this.movil = movil;
}
public String getContador() {
return contador;
}
public void setContador(String contador) {
this.contador = contador;
}
public Date getFecha_driver() {
return fecha_driver;
}
public void setFecha_driver(Date fecha_driver) {
this.fecha_driver = fecha_driver;
}
public Date getFecha_alta() {
return fecha_alta;
}
public void setFecha_alta(Date fecha_alta) {
this.fecha_alta = fecha_alta;
}
public Date getFecha_fin() {
return fecha_fin;
}
public void setFecha_fin(Date fecha_fin) {
this.fecha_fin = fecha_fin;
}
public Date getCodigo_transaccion() {
return codigo_transaccion;
}
public void setCodigo_transaccion(Date codigo_transaccion) {
this.codigo_transaccion = codigo_transaccion;
}
}
Interface Response Stored Procedue
package com.app.validate.entity;
public interface ResponseVo {
String getCode();
String getResult();
}
How could you do to read the Json with header and body? I'm new to spring boot
UPDATE
According to what Silverfang said, I created the classes said by him, but I get an error that I describe next:
BodyRequest.java
public class BodyRequest {
private String validateClientRequest;
private String movil;
public String getValidateClientRequest() {
return validateClientRequest;
}
public void setValidateClientRequest(String validateClientRequest) {
this.validateClientRequest = validateClientRequest;
}
public String getMovil() {
return movil;
}
public void setMovil(String movil) {
this.movil = movil;
}
}
HeaderRequest.java
package com.app.validate.controller;
import java.util.Date;
public class HeaderRequest {
private String country;
private String lang;
private String entity;
private String system;
private String subsystem;
private String operation;
private Date timestamp;
private String msgType;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getEntity() {
return entity;
}
public void setEntity(String entity) {
this.entity = entity;
}
public String getSystem() {
return system;
}
public void setSystem(String system) {
this.system = system;
}
public String getSubsystem() {
return subsystem;
}
public void setSubsystem(String subsystem) {
this.subsystem = subsystem;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getMsgType() {
return msgType;
}
public void setMsgType(String msgType) {
this.msgType = msgType;
}
}
RequestBodyDemo.java
package com.app.validate.controller;
public class RequestBodyDemo {
private ValidateClientRequest ValidateClient;
public ValidateClientRequest getValidateClient() {
return ValidateClient;
}
public void setValidateClient(ValidateClientRequest validateClient) {
ValidateClient = validateClient;
}
}
ValidateClientRequest
package com.app.validate.controller;
public class ValidateClientRequest {
private BodyRequest Body;
private HeaderRequest Header;
public BodyRequest getBody() {
return Body;
}
public void setBody(BodyRequest body) {
Body = body;
}
public HeaderRequest getHeader() {
return Header;
}
public void setHeader(HeaderRequest header) {
Header = header;
}
}
My Controller (Update)
package com.app.validate.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.app.validate.dao.ValidateClientAppRepository;
import com.app.validate.entity.DriverBonificados;
import com.app.validate.entity.ResponseVo;
#RestController
public class ValidateClientAppController {
#Autowired
private ValidateClientAppRepository dao;
#PostMapping(value = "/ValidateClientApp",consumes = "application/json",produces="application/json")
public ResponseVo ValidateClient(#RequestBody RequestBodyDemo req) {
System.out.println(req.getValidateClient().getBody().getMovil());
return dao.validarClienteBonifiado(req.getValidateClient().getBody().getMovil());
}
}
The error I get:
From what I understand you have changed the request format and now want the same request body to work for the same controller.
I think you were trying to add the fields to the header. What you are doing here is not the right way to do it. It should goes to header section rather than in the body section of the Postman app. But doing so, you will have to specify these header separately as these are custom headers which will be a lot of work.
Answer to your question
Going by what you were trying to do. Since now you have changed the request body. You will have to make changes in the controller class too. Now it will require three classes If you want to do it in a modular way.
The first class will be BodyRequest.java
private string validateClientRequest;
private string movil;
The next class will be HeaderRequest.java
private string country;
private string lang;
private string entity;
private string system;
private string subsystem;
private string operation;
private Date timestamp;
private string msgType;
Next class will be ValidateClientRequest.java
private HeaderRequest Header;
private BodyRequest Body;
Now for the RequestBodyDemo class;
private ValidateClientRequest ValidateClient;
Note : Use appropriate Getter and setter along with #JsonProperty if you are masking the input request data.
Once these things are done. In your controller Instead of using Entity in #RequestBody Use the class RequestBodyDemo. Once that is done. Just try printing the values just to check whether you are getting them right or not. Then use getter for fetching any value that you need.
Edit :
public ResponseVo ValidateClient(#RequestBody RequestBodyDemo req) {
System.out.println(req.getValidateClient().getBodyrequest().getMovil());
return dao.validarClienteBonifiado(req.getValidateClient().getBodyrequest().getMovil());
}
Note : Use appropriate getter method here.
I am working on an API that produces both XML and JSON responses. I have one element in the response which requires an attribute only in XML response. Also, when the value is null, the element shouldn't show up in the response for both formats.
Expectation:
XML:
<name>john</name>
<status type="text">married</status>
JSON:
"name":"john"
"status":"married"
This is my code:
/**
* POJO with bunch of LOMBOK annotations to avoid boiler-plate code.
*/
#AllArgsConstructor
#NoArgsConstructor
#Builder(toBuilder = true)
#Data
public class User implements Customer, Serializable {
private static final long serialVersionUID = 1L;
private Status status;
private String name;
/**
* Matrital status of the user.
*/
#Builder
#Value
public static class Status {
#JacksonXmlText
private String maritalStatus;
#JacksonXmlProperty(isAttribute = true)
private String type = "text";
}
}
With the above change, I am getting the correct XML response but JSON response also returns type=text
"status" : {
"maritalStatus" : "married",
"type" : "text"
}
I tried to add #JsonValue to private String maritalStatus, that solved the JSON response but it broke XML response by not adding the attribute to the element.
Can someone please help?
Probably the easiest way is to implement custom serialiser for User.Status and produce different output for different kinds of representation.
class UserStatusJsonSerializer extends JsonSerializer<User.Status> {
#Override
public void serialize(User.Status value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (gen instanceof ToXmlGenerator) {
ToXmlGenerator toXmlGenerator = (ToXmlGenerator) gen;
serializeXml(value, toXmlGenerator);
} else {
gen.writeString(value.getMaritalStatus());
}
}
private void serializeXml(User.Status value, ToXmlGenerator toXmlGenerator) throws IOException {
toXmlGenerator.writeStartObject();
toXmlGenerator.setNextIsAttribute(true);
toXmlGenerator.writeFieldName("type");
toXmlGenerator.writeString(value.getType());
toXmlGenerator.setNextIsAttribute(false);
toXmlGenerator.writeRaw(value.getMaritalStatus());
toXmlGenerator.writeEndObject();
}
#Override
public boolean isEmpty(SerializerProvider provider, User.Status value) {
return value == null || value.getMaritalStatus() == null;
}
}
Since now, you can remove extra XML annotations and register custom serialiser:
#AllArgsConstructor
#NoArgsConstructor
#Builder(toBuilder = true)
#Data
class User implements Serializable {
private static final long serialVersionUID = 1L;
private Status status;
private String name;
#Builder
#Value
#JsonSerialize(using = UserStatusJsonSerializer.class)
public static class Status {
private String maritalStatus;
private String type = "text";
}
}
Simple console app usage could look like below:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Value;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
public class JsonPathApp {
public static void main(String[] args) throws Exception {
List<User> users = Arrays.asList(
createUser("John", "married"),
createUser("Tom", null));
ObjectMapper jsonMapper = JsonMapper.builder()
.enable(SerializationFeature.INDENT_OUTPUT)
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
.build();
for (User user : users) {
System.out.println(jsonMapper.writeValueAsString(user));
System.out.println();
}
XmlMapper xmlMapper = XmlMapper.builder()
.enable(SerializationFeature.INDENT_OUTPUT)
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
.build();
for (User user : users) {
System.out.println(xmlMapper.writeValueAsString(user));
System.out.println();
}
}
private static User createUser(String name, String maritalStatus) {
return User.builder()
.name(name)
.status(User.Status.builder()
.maritalStatus(maritalStatus)
.build())
.build();
}
}
Above code prints
JSON for John:
{
"status" : "married",
"name" : "John"
}
JSON for Tom:
{
"name" : "Tom"
}
XML for John:
<User>
<status type="text">married</status>
<name>John</name>
</User>
XML for Tom
<User>
<name>Tom</name>
</User>
Notice, that we implemented UserStatusJsonSerializer#isEmpty method which defines what empty means for a Status class. Now, we need to enable JsonInclude.Include.NON_EMPTY feature in your Spring Boot application. Add below key to your application configuration file:
spring.jackson.default-property-inclusion=non_empty
If you do not want to enable inclusion globally you can enable it only for one property using #JsonInclude annotation.
#JsonInclude(JsonInclude.Include.NON_EMPTY)
private Status status;
See also:
Using Jackson to add XML attributes to manually-built node-tree
How to tell Jackson to ignore a field during serialization if its value is null?
Spring Boot: Customize the Jackson ObjectMapper
The solution to marshalling an object one way in XML, but another in JSON (different fields, etc.) was to use "mixins".
One trick is that you have to manually register the mixin, there's no magic. See below.
Mixin interface:
public interface UserStatusXmlMixin {
#JsonValue(false)
#JacksonXmlText
String getStatus();
#JacksonXmlProperty(isAttribute = true)
String getType();
}
Implementation:
#Value
public class UserStatus implements UserStatusXmlMixin {
private String status;
#JsonValue
#Override
public String getStatus() {
return status;
}
#Override
public String getType() {
return "text";
}
/**
* Returns an unmodifiable UserStatus when status is available,
* otherwise return null. This will help to remove this object from the responses.
*/
public static UserStatus of(final String status) {
return Optional.ofNullable(status)
.map(UserStatus::new)
.orElse(null);
}
}
I also had to register the "mixin" manually.
#Configuration
public class AppJacksonModule extends SimpleModule {
private static final long serialVersionUID = -1;
private final Map<Class, Class> mixinByTarget;
/**
* Construct an AppJacksonModule.
*/
public AppJacksonModule() {
super("AppJacksonModule");
this.mixinByTarget = Map.of(
UserStatus.class, UserStatusXmlMixin.class
);
}
#Override
public void setupModule(final SetupContext context) {
super.setupModule(context);
final ObjectCodec contextOwner = context.getOwner();
if (contextOwner instanceof XmlMapper) {
mixinByTarget.forEach(context::setMixInAnnotations);
}
}
Now wherever I needed to create UserStatus using UserStatus.of(..) if the input param is null, <status/> won't show up in the response.
I am using Postman console to hit service(http://localhost:8080/MyResful/countries1) and it working fine with GET method and giving the following response
[
{
"id": 1,
"countryName": "India",
"population": 10000
},
{
"id": 2,
"countryName": "Pakistan",
"population": 7000
},
{
"id": 3,
"countryName": "Nepal",
"population": 8000
},
{
"id": 4,
"countryName": "China",
"population": 20000
}
]
But it is not working with (http://localhost:8080/MyResful/countries1)POST method and giving the error:
HTTP Status 415, requestThe server refused this request because questing entity is in a format not supported by the requested resource for the requested method ().
In Postman I set header Accept and Content-type "application/JSON"
Please help me on this issue.
I am working with Spring MVC using JSON objects
Here is my controller class:
package com.ness.myrestful.controller;
import java.util.List;
import com.ness.myrestful.bean.Desh;
import com.ness.myrestful.service.DeshService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class CrudRestController {
DeshService countryService = new DeshService();
#RequestMapping(value = "/countries1", method = RequestMethod.GET, headers = "Accept=application/json")
public List<Desh> getCountries() {
List<Desh> listOfCountries = countryService.getAllCountries();
return listOfCountries;
}
#RequestMapping(value = "/country1/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Desh getCountryById(#PathVariable int id) {
return countryService.getCountry(id);
}
#RequestMapping(value = "/countries1", method = RequestMethod.POST, headers = "Accept=application/json")
public Desh addCountry(#RequestBody Desh country) {
return countryService.addCountry(country);
}
#RequestMapping(value = "/countries1", method = RequestMethod.PUT, headers = "Accept=application/json")
public Desh updateCountry(#RequestBody Desh country) {
return countryService.updateCountry(country);
}
#RequestMapping(value = "/country1/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteCountry(#PathVariable("id") int id) {
countryService.deleteCountry(id);
}
}
********************
I am working with Spring MVC using JSON objects
Here is Bean class
package com.ness.myrestful.bean;
public class Desh {
int id;
String countryName;
long population;
public Desh() {
super();
}
public Desh(int i, String countryName,long population) {
super();
this.id = i;
this.countryName = countryName;
this.population=population;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
}
********************************
I am working with Spring MVC using JSON objects
Here is my service class
package com.ness.myrestful.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.ness.myrestful.bean.Desh;
public class DeshService {
static HashMap<Integer,Desh> countryIdMap=getCountryIdMap();
public DeshService() {
super();
if(countryIdMap==null)
{
countryIdMap=new HashMap<Integer,Desh>();
// Creating some objects of Country while initializing
Desh indiaCountry=new Desh(1, "India",10000);
Desh chinaCountry=new Desh(4, "China",20000);
Desh nepalCountry=new Desh(3, "Nepal",8000);
Desh bhutanCountry=new Desh(2, "Pakistan",7000);
countryIdMap.put(1,indiaCountry);
countryIdMap.put(4,chinaCountry);
countryIdMap.put(3,nepalCountry);
countryIdMap.put(2,bhutanCountry);
}
}
public List<Desh> getAllCountries()
{
List<Desh> countries = new ArrayList<Desh>(countryIdMap.values());
return countries;
}
public Desh getCountry(int id)
{
Desh country= countryIdMap.get(id);
return country;
}
public Desh addCountry(Desh country)
{
country.setId(getMaxId()+1);
countryIdMap.put(country.getId(), country);
return country;
}
public Desh updateCountry(Desh country)
{
if(country.getId()<=0)
return null;
countryIdMap.put(country.getId(), country);
return country;
}
public void deleteCountry(int id)
{
countryIdMap.remove(id);
}
public static HashMap<Integer, Desh> getCountryIdMap() {
return countryIdMap;
}
// Utility method to get max id
public static int getMaxId()
{ int max=0;
for (int id:countryIdMap.keySet()) {
if(max<=id)
max=id;
}
return max;
}
}
Remove header and try consumes.
#RequestMapping(value = "/countries1", method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public Desh addCountry(#RequestBody Desh country) {
return countryService.addCountry(country);
}
Proper approach to fix your issue would be a global configuration as you mentioned that you are developing a json based application.
Please add following in you configuration xml file,
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
This will make sure that your JSON request object is converted to and from during server-client communication.
You can then remove the headers from your handler methods in controller.
#RequestMapping(value = "/countries1", method = RequestMethod.POST, headers = "Accept=application/json")
public Desh addCountry(#RequestBody Desh country) {
return countryService.addCountry(country);
}
To,
#RequestMapping(value = "/countries1", method = RequestMethod.POST)
public Desh addCountry(#RequestBody Desh country) {
return countryService.addCountry(country);
}
Also make sure that you have properly add jackson jars in your dependencies as well.
DTO:
public class User {
#NotNull
private String name;
#NotNull
private String password;
//..
}
Controller:
#RequestMapping(value = "/user", method = RequestMethod.POST)
public ResponseEntity<String> saveUser(#Valid #RequestBody User user) {
//..
return new ResponseEntity<>(HttpStatus.OK);
}
Default json error:
{"timestamp":1417379464584,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MethodArgumentNotValidException","message":"Validation failed for argument at index 0 in method: public org.springframework.http.ResponseEntity<demo.User> demo.UserController.saveUser(demo.User), with 2 error(s): [Field error in object 'user' on field 'name': rejected value [null]; codes [NotNull.user.name,NotNull.name,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.name,name]; arguments []; default message [name]]; default message [may not be null]],"path":"/user"}
I would like to have my custom json for each error occured. How do I accomplish that?
If you want full control over the response message in every controller write a ControllerAdvice. For example, that example transform MethodArgumentNotValidException into a custom json object:
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
/**
* Kudos http://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-adding-validation-to-a-rest-api/
*
*/
#Order(Ordered.HIGHEST_PRECEDENCE)
#ControllerAdvice
public class MethodArgumentNotValidExceptionHandler {
#ResponseStatus(BAD_REQUEST)
#ResponseBody
#ExceptionHandler(MethodArgumentNotValidException.class)
public Error methodArgumentNotValidException(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<org.springframework.validation.FieldError> fieldErrors = result.getFieldErrors();
return processFieldErrors(fieldErrors);
}
private Error processFieldErrors(List<org.springframework.validation.FieldError> fieldErrors) {
Error error = new Error(BAD_REQUEST.value(), "validation error");
for (org.springframework.validation.FieldError fieldError: fieldErrors) {
error.addFieldError(fieldError.getField(), fieldError.getDefaultMessage());
}
return error;
}
static class Error {
private final int status;
private final String message;
private List<FieldError> fieldErrors = new ArrayList<>();
Error(int status, String message) {
this.status = status;
this.message = message;
}
public int getStatus() {
return status;
}
public String getMessage() {
return message;
}
public void addFieldError(String path, String message) {
FieldError error = new FieldError(path, message);
fieldErrors.add(error);
}
public List<FieldError> getFieldErrors() {
return fieldErrors;
}
}
}
You can perform validation with Errors/BindingResult object.
Add Errors argument to your controller method and customize the error message when errors found.
Below is the sample example, errors.hasErrors() returns true when validation is failed.
#RequestMapping(value = "/user", method = RequestMethod.POST)
#ResponseBody
public ResponseEntity<String> saveUser(#Valid #RequestBody User user, Errors errors) {
if (errors.hasErrors()) {
return new ResponseEntity(new ApiErrors(errors), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(HttpStatus.OK);
}
I know this is kind of old question,
But I just run into it and I found some pretty good article which has also a perfect example in github.
Basically it uses #ControllerAdvice as Spring documentation suggests.
So for example catching 400 error will be achieved by overriding one function:
#ControllerAdvice
public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
#Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
logger.info(ex.getClass().getName());
//
final List<String> errors = new ArrayList<String>();
for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.add(error.getField() + ": " + error.getDefaultMessage());
}
for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
}
final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request);
}
}
(ApiError class is a simple object to hold status, message, errors)
One way to do it is adding message in #NotNull annotation on entity properties. And adding #Valid annotation in controller request body.
DTO:
public class User {
#NotNull(message = "User name cannot be empty")
private String name;
#NotNull(message = "Password cannot be empty")
private String password;
//..
}
Controller:
#RequestMapping(value = "/user", method = RequestMethod.POST)
public ResponseEntity<String> saveUser(#Valid #RequestBody User user) {
//..
return new ResponseEntity<>(HttpStatus.OK);
}
// Add one
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<List<YourErrorResponse>> handleException(MethodArgumentNotValidException ex) {
// Loop through FieldErrors in ex.getBindingResult();
// return *YourErrorReponse* filled using *fieldErrors*
}
#ControllerAdvice(annotations = RestController.class)
public class GlobalExceptionHandler implements ApplicationContextAware {
#ExceptionHandler(MethodArgumentNotValidException.class)
#ResponseStatus(HttpStatus.OK)
#ResponseBody
public ApplicationError validationException(MethodArgumentNotValidException e) {
e.printStackTrace();
return new ApplicationError(SysMessageEnum.MSG_005, e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
}
}
You can do something like this
#ExceptionHandler(value = MethodArgumentNotValidException.class)
protected ResponseEntity<Error> handleGlobalExceptions(MethodArgumentNotValidException ex,
WebRequest request) {
log.catching(ex);
return new ResponseEntity<>(createErrorResp(HttpStatus.BAD_REQUEST,
ex.getBindingResult().getFieldErrors().stream().map(err -> err.getDefaultMessage())
.collect(java.util.stream.Collectors.joining(", "))),
HttpStatus.BAD_REQUEST);
}
For customized the error message in JSON format then do the below steps.
- Create one #Component called CommonErrorHandler
#Component
public class CommonErrorHandler {
public Map<String,Object> getFieldErrorResponse(BindingResult result){
Map<String, Object> fielderror = new HashMap<>();
List<FieldError>errors= result.getFieldErrors();
for (FieldError error : errors) {
fielderror.put(error.getField(), error.getDefaultMessage());
}return fielderror;
}
public ResponseEntity<Object> fieldErrorResponse(String message,Object fieldError){
Map<String, Object> map = new HashMap<>();
map.put("isSuccess", false);
map.put("data", null);
map.put("status", HttpStatus.BAD_REQUEST);
map.put("message", message);
map.put("timeStamp", DateUtils.getSysDate());
map.put("filedError", fieldError);
return new ResponseEntity<Object>(map,HttpStatus.BAD_REQUEST);
}
}
-- Add InvalidException class
public class InvalidDataException extends RuntimeException {
/**
* #author Ashok Parmar
*/
private static final long serialVersionUID = -4164793146536667139L;
private BindingResult result;
public InvalidDataException(BindingResult result) {
super();
this.setResult(result);
}
public BindingResult getResult() {
return result;
}
public void setResult(BindingResult result) {
this.result = result;
}
}
- Introduce #ControllerAdvice class
#ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
#ExceptionHandler(InvalidDataException.class)
public ResponseEntity<?> invalidDataException(InvalidDataException ex, WebRequest request) {
List<FieldError> errors = ex.getResult().getFieldErrors();
for (FieldError error : errors) {
logger.error("Filed Name ::: " + error.getField() + "Error Message :::" + error.getDefaultMessage());
}
return commonErrorHandler.fieldErrorResponse("Error", commonErrorHandler.getFieldErrorResponse(ex.getResult()));
}
}
-- Use in controller with #Valid and throw exception
public AnyBeans update(**#Valid** #RequestBody AnyBeans anyBeans ,
BindingResult result) {
AnyBeans resultStr = null;
if (result.hasErrors()) {
**throw new InvalidDataException(result);**
} else {
resultStr = anyBeansService.(anyBeans );
return resultStr;
}
}
-- Output will be in JSON format
{
"timeStamp": 1590500231932,
"data": null,
"message": "Error",
"isSuccess": false,
"status": "BAD_REQUEST",
"filedError": {
"name": "Name is mandatory"
}
}
Hope this will be work. :-D
#ControllerAdvice
#RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
#Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
// ex.getBindingResult(): extract the bind result for default message.
String errorResult = ex.getBindingResult().toString();
CustomizedExceptionHandlerResponse exceptionResponse = new CustomizedExceptionHandlerResponse(
errorResult, new Date(), request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
}
}
class CustomizedExceptionHandlerResponse {
private String message;
private String status;
private Date timestamp;
// constuctor, setters, getters...
}
you can use this code to iterate through errors and build a custom error message :
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.stream.Collectors;
#ControllerAdvice
public class ExceptionHandlerController {
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorDto> handleException(MethodArgumentNotValidException ex) {
ErrorDto dto = new ErrorDto(HttpStatus.BAD_REQUEST, "Validation error");
dto.setDetailedMessages(ex.getBindingResult().getAllErrors().stream()
.map(err -> err.unwrap(ConstraintViolation.class))
.map(err -> String.format("'%s' %s", err.getPropertyPath(), err.getMessage()))
.collect(Collectors.toList()));
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(dto);
}
#Data
public static class ErrorDto {
private final int status;
private final String error;
private final String message;
private List<String> detailedMessages;
public ErrorDto(HttpStatus httpStatus, String message) {
status = httpStatus.value();
error = httpStatus.getReasonPhrase();
this.message = message;
}
}
}
This will give you a response like this in case of error :
{
"status": 400,
"error": "Bad Request",
"message": "Validation error",
"detailedMessages": [
"'yourField' should not be empty."
]
}
Add some information too.
If you use just #Valid, you need to catch BindException. If you use #Valid #RequestBody catch MethodArgumentNotValidException
Some sources:
HandlerMethodArgumentResolverComposite.getArgumentResolver(MethodParameter parameter):129 - search which HandlerMethodArgumentResolver support such parameter
RequestResponseBodyMethodProcessor.supportsParameter(MethodParameter parameter) - return true if parameter has annotation #RequestBody
RequestResponseBodyMethodProcessor:139 - throw MethodArgumentNotValidException
ModelAttributeMethodProcessor:164 - throw BindException
I know this is an old question, but i stumbled across and decided to show a cleaner way of doing what Ksokol showed.
StandardError class:
#Data
#AllArgsConstructor
#NoArgsConstructor
public class StandardError {
private Instant timestamp;
private Integer status;
private String error;
private String message;
private String path;
}
ControllerExceptionHandler class:
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<StandardError> notValid(MethodArgumentNotValidException e, HttpServletRequest request) {
String error = "Dados inválidos.";
HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
StandardError err = new StandardError(
Instant.now(),
status.value(),
error,
e.getBindingResult()
.getFieldErrors()
.stream()
.map(FieldError::getDefaultMessage)
.collect(Collectors.toSet())
.toString()
.replaceAll("\\[*]*", ""),
request.getRequestURI()
);
return ResponseEntity.status(status).body(err);
}
This way if you have a custom message on your bean validator, it will appear formatted in a error.
Result:
I am trying to use GSON to convert a java object, in list format, to JSON, I have tried a few ways, but am running into the same error.
1st attempt
List<Techinv> techs = UserUtil.getTechModels(group, org);
Gson gson = new Gson();
String json = gson.toJson(techs);
2nd attempt
List<Techinv> techs = UserUtil.getTechModels(group, org);
Type listType = new TypeToken<List<Techinv>>() {}.getType();
Gson gson = new Gson();
String json = gson.toJson(techs, listType);
3rd attempt
List<Techinv> techs = UserUtil.getTechModels(group, org);
GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder()
.addType(Techinv.class)
.registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
String json = gson.toJson(techs);
All of the above fail, with either a stackoverflow error or a can't convert java int (one of the members in my Techinv class) to a java vector, all in the GSON library.
Please tell me I am missing something simple in all these examples :)
Techinv class (if required)
package org.cchit.inv.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* The persistent class for the techinv database table.
*
*/
#Entity
#NamedNativeQueries ({
#NamedNativeQuery(name="Techinv.deleteByOrgId",
query="DELETE FROM techinv where org_id = ?")
})
#NamedQueries ({
#NamedQuery(name="Techinv.removeUser",
query="UPDATE Techinv t SET user_id = 0 where t.id = :techid"),
#NamedQuery(name="Techinv.getAllByOrg",
query="SELECT p FROM Techinv p where p.organization.liferayId = :orgid"),
#NamedQuery(name="Techinv.getById",
query="SELECT t FROM Techinv t where t.id = :id"),
#NamedQuery(name="Techinv.getByProdOrg",
query="SELECT p FROM Techinv p where p.organization.liferayId = :orgid and p.product.id = :prodid"),
#NamedQuery(name="Techinv.delete",
query="DELETE FROM Techinv t where t.id = :id")
})
#Table(name="techinv")
public class Techinv implements Serializable {
private static final long serialVersionUID = 1L;
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
#Column(name="mu_mask")
private long muMask;
#Column(name="mu_assigned_mask")
private long muAssignedMask;
#Column(name="mu_asked_assign_mask")
private long muAskedAssignedMask;
#Column(name="mu_cert_mask")
private long muWillCertMask;
#Column(name="mu_ask_will_cert_mask")
private long muAskedWillCertMask;
#Column(name="certType")
private String certType;
#Temporal(TemporalType.DATE)
#Column(name="Apply_date")
private Date applyDate;
#Temporal(TemporalType.DATE)
#Column(name="Cert_date")
private Date certDate;
#Lob()
private String notes;
//bi-directional many-to-one association to OrgUser
#ManyToOne
#JoinColumn(name="user_id")
private OrgUser orgUser;
//bi-directional many-to-one association to Organization
#ManyToOne
#JoinColumn(name="org_id")
private Organization organization;
//bi-directional many-to-one association to Product
#ManyToOne
#JoinColumn(name="prod_id")
private Product product;
//bi-directional many-to-one association to Certification
#ManyToOne
#JoinColumn(name="cert_id")
private Certification certification;
public void setCertification(Certification certification) {
this.certification = certification;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Techinv() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getNotes() {
return this.notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public OrgUser getOrgUser() {
return this.orgUser;
}
public void setOrgUser(OrgUser orgUser) {
this.orgUser = orgUser;
}
public Organization getOrganization() {
return this.organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
public long getMuMask() {
return muMask;
}
public void setMuMask(long muMask) {
this.muMask = muMask;
}
public long getMuAssignedMask() {
return muAssignedMask;
}
public void setMuAssignedMask(long muAssigned) {
this.muAssignedMask = muAssigned;
}
public Certification getCertification() {
return certification;
}
public long getMuWillCertMask() {
return muWillCertMask;
}
public void setMuWillCertMask(long muWillCertMask) {
this.muWillCertMask = muWillCertMask;
}
public long getMuAskWillCertMask() {
return muAskedWillCertMask;
}
public void setMuAskedWillCertMask(long muAskedWillCertMask) {
this.muAskedWillCertMask = muAskedWillCertMask;
}
/**
* This will set the question to true. Once asked and answered, this cannot be unset.
* #param mu
*/
public void setMuAskedAssignedMask(Mu mu) {
this.muAskedAssignedMask |= mu.getMask();
}
public boolean isCertified(Mu mu) {
return getCertification() != null && (getCertification().getMuMask() & mu.getMask()) > 0;
}
public boolean isAssigned(Mu mu) {
return (getMuAssignedMask() & mu.getMask()) > 0;
}
public boolean hasAskedToCertify(Mu mu) {
return isAssigned(mu) && !isCertified(mu) && (getMuAskWillCertMask() & mu.getMask()) > 0;
}
public boolean isWillCertify(Mu mu) {
return hasAskedToCertify(mu) && (getMuWillCertMask() & mu.getMask()) > 0;
}
public boolean hasMu(Mu mu) {
return (getMuMask() & mu.getMask()) > 0;
}
public boolean hasAskedToAssign(Mu mu) {
return (muAskedAssignedMask & mu.getMask()) > 0;
}
public String getCertType() {
return certType;
}
public void setCertType(String certType) {
this.certType = certType;
}
public Date getApplyDate() {
return applyDate;
}
public void setApplyDate(Date applyDate) {
this.applyDate = applyDate;
}
public Date getCertDate() {
return certDate;
}
public void setCertDate(Date certDate) {
this.certDate = certDate;
}
}
You should check for circular references in your serialized classes and mark the appropriate properties #Transient. Post your TechModel class if you want help with that.
edit: as using #Transient is not an option for you , use Gson's #Expose .
From the GSON user guide :
This feature provides a way where you can mark certain fields of your
objects to be excluded for consideration for serialization and
deserialization to JSON. To use this annotation, you must create Gson
by using new
GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(). The
Gson instance created will exclude all fields in a class that are not
marked with #Expose annotation.
Type listType = new TypeToken<ArrayList<Techinv>>() {}.getType();
You may try out the standard implementation of the Java API for JSON processing which is part of J2EE.
I don't have access to your beans OrgUser, Organization, Product, and Certification. So I assume that each of them comprises an id and a name.
For your List<Techinv> techList.
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<Techinv> techList = new ArrayList<Techinv>();
for (int i = 1; i <= 3; i++) {
Techinv tech = new Techinv(i, 1L, 2L, 3L, 4L, 5L, "cert",
new Date(), new Date(), "notes", new OrgUser(i, "orguser"),
new Organization(i, "organization"), new Product(i,
"product"), new Certification(i, "certification"));
techList.add(tech);
}
Techinv[] techArr = techList.toArray(new Techinv[techList.size()]);
JsonArrayBuilder techArrBuilder = Json.createArrayBuilder();
for (Techinv tech : techArr) {
JsonObjectBuilder jsonObject = Json.createObjectBuilder()
.add("id", tech.getId())
.add("muMask", tech.getMuMask())
.add("muAssignedMask", tech.getMuAssignedMask())
.add("muAskedAssignedMask", tech.getMuAskedAssignedMask())
.add("muWillCertMask", tech.getMuWillCertMask())
.add("muAskedWillCertMask", tech.getMuAskedWillCertMask())
.add("certType", tech.getCertType())
.add("applyDate", sdf.format(tech.getApplyDate()))
.add("certDate", sdf.format(tech.getCertDate()))
.add("notes", tech.getNotes())
.add("OrgUser", Json.createObjectBuilder()
.add("id", tech.getOrgUser().getId())
.add("name", tech.getOrgUser().getName()))
.add("Organization", Json.createObjectBuilder()
.add("id", tech.getOrganization().getId())
.add("name", tech.getOrganization().getName()))
.add("Product", Json.createObjectBuilder()
.add("id", tech.getProduct().getId())
.add("name", tech.getProduct().getName()))
.add("Certification", Json.createObjectBuilder()
.add("id", tech.getCertification().getId())
.add("name", tech.getCertification().getName()));
techArrBuilder.add(jsonObject);
}
JsonArray jsonArray = techArrBuilder.build();
Map<String, Object> prop = new HashMap<String, Object>() {
{
put(JsonGenerator.PRETTY_PRINTING, true);
}
};
JsonWriter jsonWriter = Json.createWriterFactory(prop).createWriter(System.out);
jsonWriter.writeArray(jsonArray);
jsonWriter.close();
The output should be:
[
{
"id":1,
"muMask":1,
"muAssignedMask":2,
"muAskedAssignedMask":3,
"muWillCertMask":4,
"muAskedWillCertMask":5,
"certType":"cert",
"applyDate":"2014-04-03",
"certDate":"2014-04-03",
"notes":"notes",
"OrgUser":{
"id":1,
"name":"orguser"
},
"Organization":{
"id":1,
"name":"organization"
},
"Product":{
"id":1,
"name":"product"
},
"Certification":{
"id":1,
"name":"certification"
}
},
{
"id":2,
"muMask":1,
"muAssignedMask":2,
"muAskedAssignedMask":3,
"muWillCertMask":4,
"muAskedWillCertMask":5,
"certType":"cert",
"applyDate":"2014-04-03",
"certDate":"2014-04-03",
"notes":"notes",
"OrgUser":{
"id":2,
"name":"orguser"
},
"Organization":{
"id":2,
"name":"organization"
},
"Product":{
"id":2,
"name":"product"
},
"Certification":{
"id":2,
"name":"certification"
}
},
{
"id":3,
"muMask":1,
"muAssignedMask":2,
"muAskedAssignedMask":3,
"muWillCertMask":4,
"muAskedWillCertMask":5,
"certType":"cert",
"applyDate":"2014-04-03",
"certDate":"2014-04-03",
"notes":"notes",
"OrgUser":{
"id":3,
"name":"orguser"
},
"Organization":{
"id":3,
"name":"organization"
},
"Product":{
"id":3,
"name":"product"
},
"Certification":{
"id":3,
"name":"certification"
}
}
]