Issue
I am developing a Web application using Vuejs/Nuxtjs
which makes call to my Java Service using the Axios
but during the call I get the error:
POST http://localhost:9001/generate 500 (Internal Server Error)
I am getting this error because my Java service type accepts the input parameter of Custom data type InputParameter
. If I change to String
then it works fine. So I would like to know what changes should I make to front-end call or to my Java Service so it can work with InputParameter type
.
Following is the Vuejs call that I am making to Java service:
const headers = { 'Content-Type': 'application/json' }
this.$axios.post('/generate', { ...JSON.parse(inputParameter) }, { headers })
.then((response) => {
console.log(JSON.stringify(response.data))
})
.catch((error) => {
console.log(error)
})
Following is my Java service method which is NOT working with custom data type InputParameter
, the call does not detect the method and execution does not go within the method:
@Path("/generate")
@Produces(MediaType.APPLICATION_JSON)
@APIResponses(value = {
@APIResponse(responseCode = "200", description = "returns list of JSON Objects"),
@APIResponse(responseCode = "500", description = "An internal Server Error occurred")
})
public String generate(final InputParameter inputParameter){
System.out.println(inputTemplate.toString());
return null;
}
If I change the above JAVA Service method input parameter data type to String then the method is detected and input is printed:
@Path("/generate")
@Produces(MediaType.APPLICATION_JSON)
@APIResponses(value = {
@APIResponse(responseCode = "200", description = "returns list of JSON Objects"),
@APIResponse(responseCode = "500", description = "An internal Server Error occurred")
})
public String generate(final String inputParameter){
System.out.println(inputTemplate);
return null;
}
I am not understanding whats wrong here. Can someone please help?
Things I have tried:
- Adding
@Consumes(MediaType.APPLICATION_JSON)
. - Changing the method to
public String generate(@RequestBody final InputParameter inputParameter)
My InputParameter
class looks something like this:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class InputParameter {
private List<String> names;
private List<String> jobs;
}
My InputParameter which I am passing to Java Service looks something like this:
{
"names":[
"Batman",
"Superman",
"Ironman"
],
"jobs":[
"Fighting",
"Fyling",
"Teching"
]
}
Solution
Posting the answer can be helpful to someone else in the future. I tried some things but nothing worked and finally following worked for me:
@POST
@Path("/generate")
@Produces(MediaType.APPLICATION_JSON)
@APIResponses(value = {
@APIResponse(responseCode = "200", description = "returns list of JSON Objects"),
@APIResponse(responseCode = "500", description = "An internal Server Error occurred")
})
public String generate(final InputParameter inputParameter){
System.out.println(inputTemplate.toString());
return null;
}
There was also one small setting that I had to change related to Jackson ObjectMapper
which is not relevant here because it's my project-specific that I missed in another class. Maybe that was the issue I was facing. Now everything is working as expected.
Answered By - BATMAN_2008
Answer Checked By - Cary Denson (JavaFixing Admin)