Jax-RS #JsonIgnore not working after adding the service endpoint to Application.getClasses - json

I'm working on my first REST API and I had it working for a minute. Basically, I have a Entity class User which has a reference to another database table Bookmark. I used #JsonIgnore at that field to exclude it from User's JSON representation:
import org.codehaus.jackson.annotate.JsonIgnore;
import javax.persistence.*;
#Entity
#Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private String id;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
#JsonIgnore
private List<Bookmark> bookmarks = new ArrayList<>();
#Column(name = "name")
private String name;
}
To get it running, I added an Application class (empty for now):
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/api")
public class RestApplication extends Application {
}
And finally, I took my good old UserService and added a REST endpoint:
#Dependent
#Named
#Path("/userService")
public class UserService {
#Inject
private UserDAO userDAO;
#GET
#Path("/getUserById/{id}")
#Produces(MediaType.APPLICATION_JSON)
public User getUserById(#PathParam("id") String id) {
return userDAO.getUserById(id);
}
}
Now I was able to access .../api/userService/getUserById/XYZ and get back a JSON representation of User including only id and name.
However, when trying to access the API from a second project running on the same machine, I stumbled upon the necessity to enable Cross-Origin Resource Sharing. I did so by following the many online guides and implemented a CorsFilter:
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
#Provider
public class CorsFilter implements ContainerResponseFilter {
#Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
}
}
It had no effect at first, but it worked when I included CorsFilter.class in the RestApplication's getClasses() method. Curling the API now showed the correct headers with CORS enabled, but the endpoints returned nothing, so I also included the UserService.class.
That's my RestApplication class right now:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/api")
public class RestApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<>();
classes.add(UserService.class);
classes.add(CorsFilter.class);
return classes;
}
}
However, now I get the infamous RESTEASY008205: JSON Binding serialization error org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.xyz.User.bookmarks, could not initialize proxy - no Session. Trying to access a different endpoint results in RESTEASY008205: JSON Binding serialization error javax.json.bind.JsonbException: Error getting value on: org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor#2ba18598, by the way, even though I can't figure out the difference between both endpoints.
At this point I'm kind of lost, it all went downhill when I tried to add the filter. Any advice on where I went wrong?
By the way, it's running on jboss-eap-7.2 and I added the following dependency to my POM to access org.codehaus.jackson.annotate.JsonIgnore:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.6.1.SP2</version>
</dependency>

According to the specification:
When an Application subclass is present in the archive, if both Application.getClasses and Application.getSingletons return an empty collection then all root resource classes and providers packaged in the web application MUST be included and the JAX-RS implementation is REQUIRED to discover them automatically by scanning a .war file as described above. If either getClasses or getSingletons returns a non-empty collection then only those classes or singletons returned MUST be included in the published JAX-RS application.
So once you provide the getClasses override, the automatic discovery is turned off, and you must register explicitely the resources and the providers, including the Jackson features.

Related

json to Instant with a field "yyyy-MM-ddThh:mm:ss" using GSON but get java.lang.NumberFormatException: For input string: "1999-08-24T00:00:00"

I am trying json parsing with gson in a small java applicaiton. I have a json string which comes from .Net business layer, has a field as "1999-08-24T00:00:00". In my model like User model, I have java.time.Instant birthDay field. With gson i am trying to get json string to my user model. Also I have a InstantDeserializer class. But when I try to convert it I got a message like java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)..
Before the instant type I was using Date class. I wrote DateDeserializer class but I know Date class is deprecated. I googled to much page. I tried many things but i didn't how to figure out. So i just want to ask where I am making mistakes. What sould I do? How can I make my code more clear or what is the best approch? If you could give some code examples, I can understand better.
Any advice is appreciated..
Here is my code..
JSON String :
{
"Value":{
"ID":"123",
"NAME":"John",
"SURNAME":"Concept",
"BIRTHDAY":"1999-08-24T00:00:00",
"PAYMENTINFORMATION":[
{
"ID":"1",
"PAYMENTINFO":"RECIEVED"
}
]
},
"Succued": true
}
UserModel class
package Models;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.time.LocalDateTime;
import java.util.Date;
import com.google.gson.annotations.SerializedName;
import java.time.Instant;
public class UserModel {
private long id;
private String name;
private String surname;
private Instant birthday;
private List<PaymentModel> paymentInformation;
//GETTER SETTER
public UserModel() {
paymentInformation= new ArrayList<>();
}
}
InstantDeserializer class
package Util;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class InstantDeSerializer implements JsonDeserializer<Instant> {
#Override
public Instant deserialize(JsonElement jelement, Type type, JsonDeserializationContext jdc) throws JsonParseException {
Instant insObj= Instant.ofEpochMilli(jelement.getAsJsonPrimitive().getAsLong());
return insObj;
}
}
And Main class
public class JSONTryMe {
public static void main(String[] args) throws Exception {
JSONObject responseJSON = new JSONObject(jsonString);
if (responseJSON.isNull("Value")) {
return;
}
GsonBuilder build = new GsonBuilder();
build.registerTypeAdapter(Instant.class, new InstantDeSerializer());
Gson gObj = build.create();
UserModel user = gObj.fromJson(responseJSON.getJSONObject("Value").toString(), UserModel.class);
System.out.println(user.getBirthday().toString());
}
}
Ant the error stackTrace is
java.lang.NumberFormatException: For input string: "1999-08-24T00:00:00"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at com.google.gson.JsonPrimitive.getAsLong(JsonPrimitive.java:206)
at Util.InstantDeSerializer.deserialize(InstantDeSerializer.java:25)
at Util.InstantDeSerializer.deserialize(InstantDeSerializer.java:21)
at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:932)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at com.google.gson.Gson.fromJson(Gson.java:817)
at Source.JSONTryMe.main(JSONTryMe.java:85)
Here are several conceptual flaws:
Birth dates should use LocalDate
Your JSON input provides ISO datetime, but your deserializer tries to read milliseconds since epoch. Use LocalDate#parse() for this

Downloading rest objects instead of showing in eclipse browser

Everytime i try to show the results from the page, it downloads the results in json format instead of showing them on page.
It starts to download when i enter the url where the objects/information is stored, instead of showing the page http://localhost:8082/spring-rest-demo/api/students
If i run the server and paste this info in postman og google chrome, it does show the correct information without downloading it as a json file.
this is how it should be
Thank you
Edit:
package com.luv2code.springdemo.rest;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.luv2code.springdemo.entity.Student;
#RestController
#RequestMapping("/api")
public class StudentRestController {
private List<Student> theStudents;
// define #PostConstruct to load the student data only once!
#PostConstruct
public void loadData() {
theStudents = new ArrayList<>();
theStudents.add(new Student("Poornima", "Patel"));
theStudents.add(new Student("Mario", "Rossi"));
theStudents.add(new Student("Mary", "Smith"));
}
// define endpoint for "/Student"-- return list of students
#GetMapping("/students")
public List<Student> getStudents() {
return theStudents;
}
// define endpoint for "/Student({studentid}"-- return list of students at index
#GetMapping("/students/{studentId}")
public Student getStudent(#PathVariable int studentId) {
// just index into the list .... keep it simple for now
return theStudents.get(studentId);
}
}
Try to add produces application/json in #GetMapping annotation.
This is a bug in recent versions of Eclipse. Hopefully the Eclipse team will fix it soon

Metric in micrometer in URI template . Some path variable needs to be replaced from URL

I want to collect metrics for particular REST API
Suppose I have a URL like /company/{companyName}/person/{id}
Is it possible to collect metrics across
/company/test/person/{id}
/compaby/test2/person/{id}
There's no out-of-the-box support for it but you can provide your own WebMvcTagsProvider to implement it via a Spring bean.
Note that it could lead to tag explosion and end up with OOM if there's any possibility to companyName path variable explosion by a mistake or attack.
In case you are using Spring and RestTemplate for http call, you can register MetricsClientHttpRequestInterceptor with your RestTemplate .
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
#Component
#AutoConfigureAfter({MetricsAutoConfiguration.class})
public class RestClientMetricConfiguration {
private final ApplicationContext applicationContext;
#Autowired
public RestClientMetricConfiguration(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
#PostConstruct
public void init() {
MetricsRestTemplateCustomizer restTemplateCustomizer =
applicationContext.getBean(MetricsRestTemplateCustomizer.class);
applicationContext.getBeansOfType(RestTemplate.class).values().forEach(restTemplateCustomizer::customize);
}
}
And use Below method provided by spring RestTemplate to make http call.
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, #Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
Type type = responseType.getType();
RequestCallback requestCallback = this.httpEntityCallback(requestEntity, type);
ResponseExtractor<ResponseEntity<T>> responseExtractor = this.responseEntityExtractor(type);
return (ResponseEntity)nonNull(this.execute(url, method, requestCallback, responseExtractor, uriVariables));
}

How to overcome "Conflicting setter definitions for property "?

I use com.fasterxml.jackson and io.swagger libraries. In my REST endpoint I use org.javamoney.moneta.Money type for a GET query. When deploying the war i get following exception 1;
I have followed this reference and wrote following code[2]; and registered it at #ApplicationPath. But still getting same issue.
Any guide would be really helpful?
#ApplicationPath("/rest")
public class RestApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
set.add(com.test.JsonMoneyProvider.class);
[2]
import javax.money.CurrencyUnit;
import javax.money.Monetary;
import javax.money.MonetaryAmountFactory;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.annotation.XmlTransient;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
#Provider
public class JsonMoneyProvider extends JacksonJsonProvider {
public JsonMoneyProvider() {
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(MonetaryAmountFactory.class, MixIn.class);
setMapper(mapper);
}
public static interface MixIn {
#JsonIgnore
#XmlTransient
MonetaryAmountFactory setCurrency(CurrencyUnit currency);
#JsonIgnore
#XmlTransient
default MonetaryAmountFactory setCurrency(String currencyCode) {
return setCurrency(Monetary.getCurrency(currencyCode));
}
}
}
1
Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "currency": javax.money.MonetaryAmountFactory#setCurrency(1 params) vs javax.money.MonetaryAmountFactory#setCurrency(1 params)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:293)
at io.swagger.jackson.ModelResolver.resolve(ModelResolver.java:246)
at io.swagger.jackson.ModelResolver.resolve(ModelResolver.java:127)
at io.swagger.converter.ModelConverterContextImpl.resolve(ModelConverterContextImpl.java:99)
at io.swagger.jackson.ModelResolver.resolveProperty(ModelResolver.java:106)
a
Simply use this annotation on the deserialization setter method to indicate Jackson wich one to use: #com.fasterxml.jackson.annotation.JsonSetter

How to test #Valid

In my entities I have some hibernate annotations for validation, like #NotEmpty, #Pattern.. and others
In my controller, on save action, it has an #Valid parameter.
But if any entity has any required field, and there is no annotation I will have problems.
So I would like to test each entity, to ensure they have the necessary notes.
Something like:
#Test(expect=IllegalArgumentException.class)
public void testAllNull() {
Person p = new Persson(); // Person name has an #NotEmpty
validator.validate(p);
}
But how to validate it? Who is called to check #Valid?
Thanks.
I found out how to check:
#Autowired
private LocalValidatorFactoryBean validator;
...
validator.validateProperty(object, propertyName)
Here is a Spring v4.1.x based example of a test validating presence and processing of the #Valid annotation and building of custom JSON response in case of an error.
jUnit
import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import javax.inject.Inject;
import java.util.List;
import static org.abtechbit.miscboard.util.JsonUtils.toJson;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {
RegistrationValidationTest.MockDependencies.class,
})
public class RegistrationValidationTest {
#Inject
MockMvc mvc;
#Test
public void validatesRegistration() throws Exception {
Registration registration = ... //build an invalid Registration object
MvcResult result = mvc.perform(post(RegistrationController.CONTEXT_REGISTER).
contentType(MediaType.APPLICATION_JSON).
content(toJson(registration))).
andExpect(status().isBadRequest()).
andExpect(content().contentType(MediaType.APPLICATION_JSON)).
andReturn();
assertThat(result.getResolvedException(), is(notNullValue()));
String content = result.getResponse().getContentAsString();
assertThat(content, is(notNullValue()));
List<Message> messages = JsonUtils.fromJson(content, new TypeReference<List<Message>>() {
});
assertThat(messages.size(), is(1));
}
public static class MockDependencies {
#Bean
public MockMvc mvc() {
return MockMvcBuilders.standaloneSetup(new RegistrationController()).build();
}
}
}
Controller
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
#Controller
public class RegistrationController
{
public static final String CONTEXT_REGISTER = "/register";
#RequestMapping(value = CONTEXT_REGISTER, method = RequestMethod.POST)
#ResponseBody
public String register(#RequestBody #Valid Registration registration) {
//perform registration
}
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<List> handleValidationException(MethodArgumentNotValidException ex) {
//Build a list of custom Message{String message;} objects
List<Message> messages = ex.getBindingResult().getAllErrors().
stream().map(e->new Message(e.getDefaultMessage())).collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).body(messages);
}
}
Spring MVC Test Framework might be a good choice. By using this, you can be assured that validations in your tests runs codes as Spring #MVC actually works.
Actually, the #Valid annotation is detected by HandlerMethodInvoker, which processes annotations on the handler methods of Spring controllers. Internally, the actual validation logic is delegated to the Validator bean depending on your application context settings. (Hibernate Validator is widely used.)
By default configuration (e.g. <mvc:annotation-driven />), LocalValidatorFactoryBean is used internally to process #Valid annotation as #Falci noted, but it may differ time to time. Instead, Spring MVC Test Framework provides the same environment as the main application uses, hence a good choice.