to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) - java

I am trying to route HttpServletRequest to another microservice where request may contain multi-part request or any normal request. but while sending I am getting the below error.
Note: I dont want to modify the request as I am trying to write some generic method.
public Object doPostCall(HttpServletRequest request, String requestURL, String rootURL)
throws URISyntaxException, IOException, ServletException {
RestTemplate restTemplate = new RestTemplate();
final String url = rootURL + requestURL;
uri = new URI(url);
try {
result2 = restTemplate.postForEntity(uri, request, Object.class);
System.out.println("after service call" + result2);
} catch (RestClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result2;
}
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
No serializer found for class java.util.Collections$3 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest["request"]->org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper["request"]->org.apache.catalina.connector.RequestFacade["attributeNames"])
Even I have tried setting this in property file. spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false, but it didn't work
My Consume controller is like below:
#PostMapping(value = "/v1/upload/{moduleName}/{fileType}", produces = "application/json")
public ResponseEntity<Object> uploadFiles(#RequestPart("file") List<MultipartFile> inputFileList,
#RequestParam(value = "createdBy", required = false) String createdBy, #PathVariable String moduleName,
#RequestParam(value = "catalogId", required = false) String catalogId,
#RequestParam(value = "catalogName", required = false) String catalogName, #PathVariable String fileType) {

Try annotating your entity class with #JsonIgnoreProperties("hibernateLazyInitializer")

Related

How to test getting parameters on the Rest service using the Post method

I'm trying to test getting parameters for processing a request using the Post method
#RestController
#RequestMapping("api")
public class InnerRestController {
…
#PostMapping("createList")
public ItemListId createList(#RequestParam String strListId,
#RequestParam String strDate) {
…
return null;
}
}
test method
variant 1
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class InnerRestControllerTest {
#LocalServerPort
private int port;
#Autowired
private TestRestTemplate restTemplate;
#Test
void innerCreatePublishList() {
String url = "http://localhost:" + this.port;
String uri = "/api/createList";
String listStr = "kl";
String strDate = "10:21";
URI uriToEndpoint = UriComponentsBuilder
.fromHttpUrl(url)
.path(uri)
.queryParam("strListId", listStr)
.queryParam("strDate ", strDate)
.build()
.encode()
.toUri();
ResponseEntity< ItemListId > listIdResponseEntity =
restTemplate.postForEntity(uri, uriToEndpoint, ItemListId.class);
}
}
variant 2
#Test
void createList() {
String uri = "/api/createList";
String listStr = "kl";
String strDate = "10:21";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uri)
.queryParam("strListId", listStr)
.queryParam("strDate ", strDate);
Map<String, String> map = new HashMap<>();
map.put("strListId", listStr);//request parameters
map.put("strDate", strDate);
ResponseEntity< ItemListId > listIdResponseEntity =
restTemplate.postForEntity(uri, map, ItemListId.class);
}
Update_1
In my project exceptions is handled thus:
dto
public final class ErrorResponseDto {
private String errorMsg;
private int status;
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
LocalDateTime timestamp;
...
handler
#RestControllerAdvice
public class ExceptionAdviceHandler {
#ExceptionHandler(value = PublishListException.class)
public ResponseEntity<ErrorResponseDto> handleGenericPublishListDublicateException(PublishListException e) {
ErrorResponseDto error = new ErrorResponseDto(e.getMessage());
error.setTimestamp(LocalDateTime.now());
error.setStatus((HttpStatus.CONFLICT.value()));
return new ResponseEntity<>(error, HttpStatus.CONFLICT);
}
}
In methods, where necessary, I throw a specific exception...
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved
[org.springframework.web.bind.MissingServletRequestParameterException:
Required String parameter 'strListId' is not present]
Who knows what the error is. Please explain what you need to add here and why ?
Let's take a look on declarations of postEntity:
postForEntity(URI url, Object request, Class<T> responseType)
...
postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
As you can see, first argument is either URI or String with uriVariables, but second argument is always request entity.
In you first variant you put uri String as URI and then pass uriToEndpoint as request entity, pretending that it is request object. Correct solution will be:
ResponseEntity<ItemListId> listIdResponseEntity =
restTemplate.postForEntity(uriToEndpoint, null, ItemListId.class);
Addressing your comments.
If server responded with HTTP 409, RestTemplate will throw exception with content of your ErrorResponseDto. You can catch RestClientResponseException and deserialize server response stored in exception. Something like this:
try {
ResponseEntity<ItemListId> listIdResponseEntity =
restTemplate.postForEntity(uriToEndpoint, null,
ItemListId.class);
...
} catch(RestClientResponseException e) {
byte[] errorResponseDtoByteArray = e.getResponseBodyAsByteArray();
// Deserialize byte[] array using Jackson
}

Sending MultipartFile with some parameter to rest api in java

I am using rest api to send file with some data.
Below is the signature of API
#RequestMapping(value = "/file", method = RequestMethod.POST)
public ModelAndView uploadFile(HttpServletRequest request,
HttpServletResponse response,
#RequestParam(required = false) String wfid,
#RequestParam String ssoToken,
#RequestParam(required = false) String typeMedia,
#RequestParam(required = false) String synopsisParam,
#RequestParam(required = false) String slideShowParam,
#RequestParam(required = false) String embedInContentParam,
#RequestParam(required = false) boolean sizeRestrictionRequiredFlag,
#RequestParam MultipartFile file) throws Exception {
logger.info("SSO - " + ssoToken);
return "Output";
}
Below is my method in which i am creating POST request
void myMethod(String restAPI, String ssoId, byte[] imageByte){
PostMethod post = null;
HttpClient httpClient = new HttpClient();
try {
post = new PostMethod(restAPI);
Part[] parts = new Part[] { new FilePart("myImage.JPG", new ByteArrayPartSource("myImage.JPG", imageByte))};
HttpMethodParams par = post.getParams();
par.getDefaults().setParameter("ssoToken", ssoId);
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, post.getParams());
ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
multipartRequestEntity.writeRequest(requestContent);
post.setRequestEntity(multipartRequestEntity);
post.setRequestHeader("content-type", multipartRequestEntity.getContentType());
/*NameValuePair[] postParameters = new NameValuePair[]{new NameValuePair("ssoToken", ssoId)};
post.setRequestBody(postParameters);*/
int status = httpClient.executeMethod(post);
String responseBody = post.getResponseBodyAsString();
} catch(Exception e){
} finally{
if(post != null){
post.releaseConnection();
}
}
}
I am getting 400 - Required String parameter 'ssoToken' is not present
When i tried commented code
/NameValuePair[] postParameters = new NameValuePair[]{new NameValuePair("ssoToken", ssoId)};
post.setRequestBody(postParameters);/
in above method and send NameValue pair as response body above exception is resolve but Multipart file exception occur.
Can someone help how create request to rest controller mentioned above.
Kindly let me know the issue before down vote.
Thanks in advance
The reason this is not working, is because, the Name-Value in parameters do not isolate customer Request Parameters that your Rest api needs.
Essentially, java wouldn't know where to look into, to extract the RequestParam "ssoToken" that your rest controller expects.
What you can do, however is this -
Part[] parts = new Part[] { new FilePart("file",file), new StringPart("ssoToken",ssoToken) };
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, par);
HttpMethodParams par = post.getParams();
Then proceed with the rest of the code as is.
The controller isolates the parts and searches for the "ssoToken" object, which it will now find!
This, I beleive should fix this problem!

Swagger example post body - how to show JSON Body- Swagger-annotations

Requirement: I have a POST method which takes the input JSON as a String and passes it to another microservice. I don't want to create an Object (Bean) of this input JSON.
method:
#ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
#RequestMapping(name = "xxx" value ="/hello" ..)
#ApiResponses(..)
public #ResponseBody String getXXX (#Apiparam(name="JSONrequest", required = true) #RequestBody String JSONrequest){
}
Problem:
The generated Swagger doesn't show the input as a JSON model where all the JSON attributes are displayed.
Expectation:
I want to display my Swagger Something like this :
Definately I am missing the key thing. Any thoughts?
If changing from String to a concrete object is not okay (although that's what I would recommend you to do since it's cleaner), you can try using #ApiImplicitParams (check out their documentation)
#ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
#ApiImplicitParams({
#ApiImplicitParam(name = "Object", value = "Object to be created", required = true, dataType = "your.package.BodyClass", paramType = "body")
})
#RequestMapping(name = "xxx" value ="/hello" ..)
#ApiResponses(..)
public #ResponseBody String getXXX (#Apiparam(name="JSONrequest", required = true) #RequestBody String JSONrequest){
}
(not sure if you still need the #Apiparam(name="JSONrequest", required = true) bit from the method parameter)
It's an old question but since I haven't found a solution online here how I to customized the example value in the swagger documentation produce automatically by the java annotations.
I use swagger 2.0 and springfox.version 2.10.5.
The Idea is documenting the class of the request parameter that has the #RequestBody annotation. for example my method is
#ApiOperation(
value = "Start ListBuilder extraction",
response = ExtractionLogEntity.class,
produces = "application/json"
)
#PostMapping("/extraction/start")
public ExtractionLogEntity startTask(
#RequestBody(required = true) ExtractionRequest request,
In order to expose request json object example I added a #ApiModelProperty(example = "...") annotation to the properties of ExtractionRequest .
#ApiModelProperty(example = "[{ 'field':'value'}]")
#NotNull
private List<ListBuilderFieldEntity> fields;
#ApiModelProperty(example = "1000")
private String ied;
#ApiModelProperty(example = "US")
private String codebase;
And that's the result
I had the similar issue. My Service Class takes #RequestBody argument in String.
So, what I did :
Created a POJO and used #RequestBody annotation with it instead of inputString.
#RequestMapping(value = "/api/entity/{entityId}/user/query", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody
ResponseEntity<String> queryUser(#PathVariable("entityId") String entityId,
#RequestBody QueryUserJsonSchemaPOJO queryUserJsonSchemaPOJO, String inputString,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return userService.queryUserService(inputString, entityId, request);
}
Created an AOP with #Around annotation which update the inputString argument.
#Around(value = "execution(* com.athmin.rest.UserController.*(..)) || execution(* com.athmin.rest.CityController.*(..)), and args(..) " +
" && #annotation(com.athmin.annotations.JSONSchemaFileName) ")
public Object validateRequestBodyAgainstJsonSchema(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object[] modifiedArgs = proceedingJoinPoint.getArgs();
for (Object o : proceedingJoinPoint.getArgs()) {
if (o instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) o;
requestBody = httpServletRequest.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
}
});
for (int i = 0; i < modifiedArgs.length; i++) {
if (modifiedArgs[i] == null) { // Only inputString is null in my case
modifiedArgs[i] = requestBody;
}
}
proceedingJoinPoint.proceed(modifiedArgs);
}

Spring request params to accept only Integers

I am working on an spring restful endpoint which accepts page range(start & end page number). I want my request params- pageStart and pageEnd to accept only integers. When I pass 'pageStart = a' through postman I get below error:
#RequestMapping(value = "/{accNumber}/abc/xyz", method = RequestMethod.GET)
#Loggable
#ResponseBody
public RestResponse<Class1> getData(
#Loggable #PathVariable(value = "accNumber") String accNumber,
#RequestParam(value = "pageStart", required = false, defaultValue = "0") Integer pageStart,
#RequestParam(value = "pageEnd", required = false, defaultValue = "10") Integer pageEnd,
HttpServletResponse response) throws Exception {
Class1 class1 = new Class1();
class1 = retrieveData(accNumber, pageStart, pageEnd);
RestResponse<Class1> restResponse = new RestResponse<Class1>(
class1);
return restResponse;
}
The request is not valid [Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: \"a\"]
How do I handle this exception and let the user know that he should pass only integers?
You can handle it in two ways
1) Using exception handler method
Have a method in the controller
#ExceptionHandler({Exception.class})
public ModelAndView handleException(Exception ex) {
ModelAndView model = new ModelAndView("Exception");
model.addObject("exception", ex.getMessage());
return model;
}
http://www.codejava.net/frameworks/spring/how-to-handle-exceptions-in-spring-mvc
2) Use String parameter
Use String as the type for all #PathVariable and #RequestParameter parameters then do the parsing inside the handler method.

Tunneling MultipartFile

I have a spring controller that accepts a class named FileUploadBean on POST. The controller method looks like that:
First Controller:
#RequestMapping(value = "/upload", method = RequestMethod.POST)
#ResponseBody
public ResponseEntity<byte[]> uploadFile(final FileUploadBean fileUploadBean) throws IOException {
// Some code that works fine here
}
One of the FileUploadBean properties is of type MultipartFile.
Now, I'm trying to add some sort of wrapper controller (that will run on another server) that also accepts FileUploadBean and just forwards the request to the first controller:
Second (wrapper) Controller:
#RequestMapping(value="/upload", method = RequestMethod.POST)
#ResponseBody
public ResponseEntity<byte[]> uploadImage(final FileUploadBean fileUploadBean) throws IOException {
ResponseEntity<byte[]> response = restTemplate.postForEntity([first controller url here], fileUploadBean, byte[].class);
return response;
}
When I'm sending the request to the first controller I get:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: No serializer found for class
java.io.FileDescriptor and no properties discovered to create
BeanSerializer (to avoid exception, disable
SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain:
com.outbrain.images.beans.FileUploadBean["file"]->org.springframework.web.multipart.commons.CommonsMultipartFile["fileItem"]->org.apache.commons.fileupload.disk.DiskFileItem["inputStream"]->java.io.FileInputStream["fd"]);
nested exception is
com.fasterxml.jackson.databind.JsonMappingException: No serializer
found for class java.io.FileDescriptor and no properties discovered to
create BeanSerializer (to avoid exception, disable
SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain:
com.outbrain.images.beans.FileUploadBean["file"]->org.springframework.web.multipart.commons.CommonsMultipartFile["fileItem"]->org.apache.commons.fileupload.disk.DiskFileItem["inputStream"]->java.io.FileInputStream["fd"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal
How can I make this request work?
Well, after some struggling this is how I solved it. That's what I did in the second controller:
#RequestMapping(value = "/upload", method = RequestMethod.POST)
public #ResponseBody
ResponseEntity<byte[]> uploadImage(final FileUploadBean fileUploadBean) throws Exception {
File file = null;
try {
final MultiValueMap<String, Object> requestParts = new LinkedMultiValueMap<>();
final String tmpImageFileName = IMAGE_TMP_DIR + fileUploadBean.getFile().getOriginalFilename();
file = new File(tmpImageFileName);
fileUploadBean.getFile().transferTo(file);
requestParts.add("file", new FileSystemResource(tmpImageFileName));
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/form-data"); // Sending it like the client-form sends it
ResponseEntity<byte[]> response = restTemplate.exchange(ImageUrlUtils.getUploadUrl(), HttpMethod.POST, new HttpEntity<>(requestParts, headers),
byte[].class);
return new ResponseEntity<>(response.getBody(), response.getStatusCode());
} catch (Exception ex) {
return new ResponseEntity<>((ex.getMessage).getBytes("UTF-8"),
HttpStatus.INTERNAL_SERVER_ERROR);
} finally {
if (file != null && file.exists()) {
file.delete();
}
}
}
I debug previous answer, and found this solution without save file to file system
#PostMapping(value = "/upload")
public ResponseEntity<Object> upload(MultipartHttpServletRequest request) throws Exception {
final MultiValueMap<String, Object> requestParts = new LinkedMultiValueMap<>();
request.getParameterMap().forEach((name, value) -> requestParts.addAll(name, asList(value)));
request.getMultiFileMap().forEach((name, value) -> {
List<Resource> resources = value.stream().map(MultipartFile::getResource).collect(toList());
requestParts.addAll(name, resources);
});
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestParts, request.getRequestHeaders());
return restTemplate.exchange(ImageUrlUtils.getUploadUrl() + "?" + request.getQueryString(),
request.getRequestMethod(), requestEntity, Object.class);
}

Categories