My rest api is.
#PutMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUEpath="/{referenceNumber}")
public void updateCard(#RequestHeader(value = tenantId) String theTenantId,
#PathVariable String referenceNumber,#RequestParam(value = "card")MultipartFile multipartFile,HttpServletRequest request)
I need to check the condition like without browsing the file.
my input format is
Headers :
tenantId : ***
Body : selecting "formdata" (Postman), "multipart-formdata" (AdvancedRestClient)
card : without browsing file
Then i am getting the following error in Postman.
{
"timestamp": 1549351840816,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.multipart.support.MissingServletRequestPartException",
"message": "Required request part 'card' is not present",
"path": "/app-1.5.0/1.5/references/34a236d7-9305-402f-959d-8c83d5ededbb"
}
If i try in the AdvancedRest client with same input
I am getting the different error.
{
"timestamp": 1549352119229,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'null' not supported",
"path": "/app-1.5.0/1.5/references/34a236d7-9305-402f-959d-8c83d5ededbb"
}
Is there any reason for different outputs and can i check the api without
browsing file.
#PutMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUEpath="/{referenceNumber}")
public void updateCard(#RequestHeader(value = tenantId) String theTenantId,
#PathVariable String referenceNumber,#RequestPart(required = false,value = "card")MultipartFile multipartFile,HttpServletRequest request){
}
User #RequestPart instead of #RequestParam
Related
I have a spring mvc handler like this:
#PostMapping("jwtToken")
fun jwtToken(#RequestBody body: JWTToken)
{
val token = body.token
if(token.isNullOrBlank())
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Empty token")
}
If i send it an incorrect input, that triggers the exception, i get a reponse body like this:
{
"timestamp": "2020-10-30T03:41:20.305+00:00",
"status": 401,
"error": "Unauthorized",
"message": "",
"path": "/auth/jwtToken"
}
Why is the 'message' field empty in the response when i did assign a message to the exception? How do i set the message field
It might be related to the updated behaviour of the Spring Boot.
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#changes-to-the-default-error-pages-content
server.error.include-message=always
in .properties should do the trick but I prefer to use my own extended classes like this:
class CustomException(message: String): Exception(message) { ... }
I have a method which gives response using Mono.fromfuture(result) and which throws CustomException with 400 as status.
Now in my service class, when I call that method, the error and code I am throwing there is not getting propagated to my client(postman). Only the message is what I am seeing.
I am getting this below format: -
{
"timestamp": "2019-02-01T11:13:32.748+0000",
"path": "/some_url",
"status": 500,
"error": "Internal Server Error",
"message": "Unable to fetch Response"
}
Expectation (what I want to achieve) : -
{
"timestamp": "2019-02-01T11:13:32.748+0000",
"path": "/some_url",
"status": 400, // (my own status)
"error": "INVALID_ARGUMENT", // (my own error)
"message": "Unable to fetch Response"
}
My Code:-
public Mono<ResponseObject> fetchResponse(arg) {
return Mono.just(somedata which will give exception)
.flatMap(k -> callExternalService(k))
.onErrorMap(e -> Mono.error(
new BusinessException("Unable to fetch Response", e))
*/* e here is giving :-
"code" : 400,
"message" : "Request contains an invalid argument.",
"status" : "INVALID_ARGUMENT" */*
}
Have you looked at the documentation of the Response class?
You can create a Response of your own, using the static methods in the doc, and send it, instead of Mono.error, in the onErrorMap.
You have to return something like below:
ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(klass, Klass.class);
ServerResponse.status(status).build();//read doc and set for yourself
You can also check this link.
I am developing reactive-spring-boot web service and also using reactive-mongodb to store data. I want to return my customize response for API. example as below:
[
{
"result": [
{
"code": "",
"data": "",
"error": ""
}
]
}
Sample Method:
#PostMapping(value="/addEmployee")
public Mono<Response> addEmployeeDetails(Employee employee){
Response response = new Response();
if(employee.getEmpID() == null){
return response(getResponse());
}
Repository.save(employee);
return response(getResponse());
}
As you see in the code I have response() method which builds the required response.But it mono gives default response as below:
{
"timestamp": "2018-09-29T16:23:21.287+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Internal Server Error",
"path": "/employee"
}
I am not able to set my own status code and error message.
I have this repository
#RepositoryRestResource(collectionResourceRel = "party_category", path = "party_category")
public interface PartyCategoryRepository extends PagingAndSortingRepository<PartyCategory, Integer>
{
}
When I try to persist with:
curl -X POST -H "Content-Type: application/json" http://localhost:8080/party_category -d "{\"description\":\"Mock description 3 modif\"}"
I get a 500 Error instead a 400:
{
"timestamp": 1438616019406,
"status": 500,
"error": "Internal Server Error",
"exception": "javax.validation.ConstraintViolationException",
"message": "Validation failed for classes[main.entity.party.PartyCategory] during persist time for groups [javax.validation.groups.Default, ]\\\nList of constraint violations:[\\\n\\\tConstraintViolationImpl{interpolatedMessage='no puede estar vacĂo', propertyPath=name, rootBeanClass=class main.entity.party.PartyCategory, messageTemplate='{org.hibernate.validator.constraints.NotBlank.message}'}\\\n]",
"path": "/party_category"
}
What can I do to get a friendly response like this?:
{
"field":"name",
"exception":"Field name cannot be null"
}
How can I return a 400 http code?
It seems you need to map your specific Exception to the HTTP Status:
#Controller
public class ExceptionHandlingController {
// Convert a predefined exception to an HTTP Status code
#ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Data integrity violation") // 409
#ExceptionHandler(javax.validation.ConstraintViolationException.class)
public void error() {
// Nothing to do
}
}
I'm trying to access calendar details but I seem to get a invalid credentials error.
I access the following url: "https://www.googleapis.com/calendar/v3/calendars/{google_id}/events"
like this :
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url_);
get.addHeader("Authorization","OAuth" + auth_token);
try {
HttpResponse response = client.execute(get);
What extra headers should I put there? I'm accessing it from an android application. If it matters I got my auth_token by an activity started with AccountManager.newChooseAccountIntent().
I'm getting the following json:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
There should be a space between OAuth and the token in your header.