Custom error is not rendered as hal in Spring Boot 1.3 and Spring hateoas 0.19 - rest

Initially I used Spring Boot 1.2 and Spring hateoas in my project, and I need to customize error message. So I created our class instead of the native VndErrors and VndError.
I created a class extends VndErrors.VndError.
public class MyError extends VndErrors.VndError{
//add some my custom fields
}
And antoher class to wrap the MyError.
public class ErrorDetails{
int total;
#JsonProperty("_embedded")
Map<String, List<MyError>> errors;
public ErrorDetails(List<MyError> err){
this.total=err.size();
errors.put("errors", err);
}
}
All exception are hanleded in a #ContrllerAdvice class. I used a custom Jackson2ObjectMapperBuilder to configure ObjectMapper in our project.
When I used Spring 1.2, it was rendered as expected. As following.
{
"total": 1,
"_embedded":{
"errors":[
{
//feilds,
_links:{
"self":""
}
}
]
}
}
But when upgraded to Spring Boot 1.3, it does not work as excepted.
The _links rendered as links, and the content type is application/json in the debug info.
Stage 1:
I am trying to create a simple pojo with a List of Link, it does not work.
public class ErrorDetails{}//pojo includes fields
public class MyError{
//add some my custom fields
#JsonUnwrapped
ErrorDetails content;
List<link> links;
}
public class ErrorResources{
int total;
#JsonProperty("_embedded")
Map<String, List<MyError>> errors;
public ErrorResources(List<MyError> err){
this.total=err.size();
errors.put("errors", err);
}
}
I found some related issues on github of Spring Hateoas project.
https://github.com/spring-projects/spring-hateoas/issues/279
https://github.com/spring-projects/spring-hateoas/issues/324
https://github.com/spring-projects/spring-hateoas/issues/288
I tried one of the suggestions of the issues above, when added #JsonSerialze(using=Jackson2HalModule.HalLinkListSerializer) on links of MyError class.
Got message similar with can not find the correct HttpMessageConverter, the content type of result is application/ocect(binary).
I also tried set the default contentType or default viewResolver to MappingJackson2JsonView, all did not change the result.
Whend I added a custom MappingJackson2HttpMessageConverter in my config:
#Bean
#Order(1)
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
ObjectMapper halObjectMapper=ctx.getBean("_halObjectMapper", ObjectMapper.class);
MappingJackson2HttpMessageConverter jacksonMessageConverter =
new MappingJackson2HttpMessageConverter();
jacksonMessageConverter.setObjectMapper(halObjectMapper);
jacksonMessageConverter.setSupportedMediaTypes(
Arrays.asList(MediaTypes.HAL_JSON, MediaType.APPLICATION_JSON_UTF8, MediaType.ALL));
return jacksonMessageConverter;
}
The error result is rendered as expected. But I do not think it is the correct way, because I used MediaType.ALL here. And it caused another big problem.
I used TestRestTemplate to test my rest APIs. The restTemlate tried to treat the input data as XML. I saw in the exception it indicated it tried to invoke a XmlHttpMessageConverter to process the content(it is json), even I have set the accept header as application/json. Of course, before I upgraded to Spring Boot 1.3 stack, it worked.
Stage 2:
I tried to use Resources and Resource to wrap the error result.
public class ErrorDetails{}//pojo includes error description fields
public class ErrorResource extends Resource<ErrorDetails>{
}
public class ErrorResources extends Resources<ErrorResource>{
}
public class ErrorMessage {
int total;
ErrorResources errors;
}
Spring still can not render the error result as hal format, it is application/json. When I added
#JsonSerialze(using=Jackson2HalModule.HalResourcesSerializer) on ErrorResources class, it raised an exception which complained the HalResourcesSerializer does not has a default constructor.
In the #ControllerAdvice class, I have tried to set the method return type to ErrorMessage and a wrapper ResponseEntity , it does not work.
Finally, my question is how to render the response body in a #ControllerAdvice same as the one in a normal #RestController? Why it does not work in a #ControllerAdvice class?
Is there a simple workaroud for this issue?

Related

Drilling down model objects on Swagger

My application is a restful api and its integrated with Swagger and OpenAPI.
I have generated all Java stubs using OpenAPI YAML file and everything is working fine.
But when i try o drill down model objects on Swagger then it cannot locate some of objects although there are part of project as project compiles fine.
As shown in below screenshot, drilldown fails to locate COnfiguration object.
Any ideas on how to resolve this.
Edit:
I have a restful webservice and i generate all the java stubs [Data transfer objects] from a YAML file using openapi-generator plugin. This plugin automatically generates a class OpenAPIDocumentationConfig and following are the details of the class. After this setup, models are automatically generated in Swagger UI.
Also want to add that I am using OpenAPI 3.0 but i need to split Object definitions into multiple files. So i am referring to them using definitions as i don't believe component schemas can be split into multiple files.
#Configuration
#EnableSwagger2
public class OpenAPIDocumentationConfig {
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("ABC Service")
.description("ABC Service")
.license("")
.licenseUrl("http://unlicense.org")
.termsOfServiceUrl("")
.version("1.0.0")
.contact(new Contact("","", "xyz#abc.com"))
.build();
}
#Bean
public Docket customImplementation(ServletContext servletContext, #Value("${openapi.studioVALService.base-path:}") String basePath) {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.x.y.z"))
.build()
.pathProvider(new BasePathAwareRelativePathProvider(servletContext, basePath))
.directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
.directModelSubstitute(java.time.OffsetDateTime.class, java.util.Date.class)
.apiInfo(apiInfo());
}
class BasePathAwareRelativePathProvider extends RelativePathProvider {
private String basePath;
public BasePathAwareRelativePathProvider(ServletContext servletContext, String basePath) {
super(servletContext);
this.basePath = basePath;
}
#Override
protected String applicationPath() {
return Paths.removeAdjacentForwardSlashes(UriComponentsBuilder.fromPath(super.applicationPath()).path(basePath).build().toString());
}
#Override
public String getOperationPath(String operationPath) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath("/");
return Paths.removeAdjacentForwardSlashes(
uriComponentsBuilder.path(operationPath.replaceFirst("^" + basePath, "")).build().toString());
}
}
}
EDIT 2:
I moved all definitions to components and schemas but they are still split in multiple files and are referring to components across files but still i get the same error.
If you are using OpenAPI 3 you should put schemas that you want to reuse inside components. To refeer to it you must use refs like:
$ref: "#/components/schemas/EquityOptionConfigurationDO"

Error resolving template - url mapping fails

I receive the following error message:
Error resolving template [catalog/getCatalogItemFromCatalog/catalogItemId/3916677], template might not exist or might not be accessible by any of the configured Template Resolvers
I am trying to reach my service and the method using this url:
http://192.168.99.100:31003/catalog/getCatalogItemFromCatalog/catalogItemId/3916677
Controller:
#Controller
#RequestMapping("catalog")
public class CatalogController {
#GetMapping("/getCatalogItemFromCatalog/catalogItemId/{catalogItemId}")
public CatalogItem getCatalogItemFromCatalog(#PathVariable Integer catalogItemId){
List<Catalog> catalogs = getAllCatalogs();
Optional<CatalogItem> optionalCatalogItem = Optional.empty();
for(Catalog catalog : catalogs){
optionalCatalogItem = catalog.getCatalogItems().stream().filter(it -> it.getCatalogItemId().equals(catalogItemId)).findFirst();
}
return optionalCatalogItem.orElse(null);
}
#GetMapping("/system/ipaddr")
public String getIpAddr() {
List<String> response;
response = runSystemCommandAndGetResponse(IP_ADDR);
return new Gson().toJson(response);
}
}
When I curl
http://192.168.99.100:31003/catalog/system/ipaddr
I have no issues.
I am testing for hours now and nothing seems to work, I have no idea why its failing tho.
you have #Controller on your class which means spring will try to resolve the return type of all your methods inside the controller using all the available templateResolvers.
by using #ResponseBody spring will wrap the return type inside the response (after converting it) directly then returns it to the client, it's similar to using #RestController instead #Controller

Jersey 2 with Jackson serialisation issue

Tools :
Weblogic 12c
Jersey 2.21.1
Jackson 2
public class Profile implements Serializable
{
private List<Status> orderStatus;
public void setOrderStatus(List<Status> orderStatus)
{
this.orderStatus = orderStatus;
}
public void getOrderStatus()
{
return orderStatus;
}
I have a simple POJO class as above.
I am facing below issue while using Jersey 2 with Jackson.
1)When Profile class is serialised , the JSON gets created as : {"OrderStatus":[{}]}
2)So the key generated is OrderStatus and not orderStatus
3)When this JSON gets de-serialised , it throws error -
Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "OrderStatus" since it cannot
find field with OrderStatus but has field as orderStatus
I have tried adding :
#JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY,getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
hoping that serialisation will only use fields as a key (and will not use property) and JSON will be generated as {"orderStatus":[{}]} .
But it is not working and same error is thrown.
Is there any way we can serialise POJO with key as fields and not properties.
Can anybody please help here ?
If you want to make sure orderStatus is the name use specific JsonProperty :
#JsonProperty("orderStatus")
public void getOrderStatus()
#JsonProperty (also indicates that property is to be included) is used to indicate external property name

Error returning custom object via GET method

I am trying to run a simple example in Jersey which has a GET method and returns a Custom object.
on calling GET I am getting following error
MessageBodyWriter not found for media type=text/plain
I have looked into few answers on stackoverflow where they are suggesting it to put a default constructor and jackson dependency in pom.xml. I have already done that but no luck. Can some one please suggest what I am doing wrong.
Resource Class
#Path("customdatatyperesource")
public class CustomDataTypeResource {
#GET
public CustomResource1 get(#Context UriInfo ui) {
return new CustomResource1();
}
}
Custom Class
#XmlRootElement
public class CustomResource1 {
#XmlElement
String res;
public CustomResource1() { }
#Override
public String toString(){
return "Custom : "+res;
}
}
pom.xml
Thanks
So I figured out that error is not in the code but in the request sent.
When I send the request with header accept: text/plain
I am getting the error MessageBodyWriter not found for media type=text/plain
The resolution is accept header needs to match with what resource can produce.
In this case our resource is capable of producing XML or JSON
A better and more comprehensive way to write this code would be to put produce annotation on the methods.
#Produces(MediaType.TEXT_XML)
and put correct accept header such as
accept: application/json

Issues with Restful Jersey and JSON

I have a strange issue and didn't find any information about it at all.
Having a simple POJO like (simplified..)
#XmlRootElement
public class Bill {
List<Position> positions
.. getter/setter
}
#XmlRootElement
public class Position {
.. some simple properties with getters/setters
}
I am unable to call a RESTful Service using instances of these classes. I'm getting real weird errors I don't really understand.
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token
The funny thing is, when I just test serialization/deserialization using Jackson Object mapper directly, it works as expected!
ObjectMapper mapper = new ...
mapper.writeValue(stringWriter, bill);
mapper.readValue(stringWriter.toString(), Bill.class);
This works perfectly. So I guess the POJO itself is OK and Jackson is able to handle the JSON-String.
Calling the RESTful service using the same Bill instance fails with the error mentioned above. I see it is using Jackson as well, here is part of stack trace:
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:198)
at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:103)
at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:93)
at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:230)
And here is how the RESTful Application is configured:
#javax.ws.rs.ApplicationPath("rest")
public class ApplicationConfig extends Application {
public Set<Class<?>> getClasses() {
return getRestResourceClasses();
}
/**
* Do not modify this method. It is automatically generated by NetBeans REST support.
*/
private Set<Class<?>> getRestResourceClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
resources.add(rest.RestAPI.class);
// following code can be used to customize Jersey 1.x JSON provider:
try {
Class jacksonProvider = Class.forName("org.codehaus.jackson.jaxrs.JacksonJsonProvider");
resources.add(jacksonProvider);
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
return resources;
}
}
Do you have any idea what I'm missing?
I generated the service and the client with NetBeans. Oh and it works when I use XML instead of JSON.
Any help would be very much appreciated.
I'm sorry but after hours of testing and debugging I finally found the cause of the problem.
Still I would be very interested why this is?
Commenting out the following code did the trick:
// following code can be used to customize Jersey 1.x JSON provider:
try {
Class jacksonProvider = Class.forName("org.codehaus.jackson.jaxrs.JacksonJsonProvider");
resources.add(jacksonProvider);
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null, ex);
}