Unable to show XML data - spring

Hi my need is to show both xml and json data .
I am able to see this in local by JaxB but unable to see same code in server.
When ever I deploy that to server I got this error.
I don't know how to solve this error.
Unable to solve this, Tried a lot but Nothing happened , in local everything is fine, but when it comes to server it shows different exception.
Error 500: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Could not instantiate JAXBContext for class [class com.rest.model.ExerciseInstructionsList]: null; nested exception is javax.xml.bind.JAXBException - with linked exception: [com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Class has two properties of the same name "exerciseList" this problem is related to the following location: at public java.util.List com.rest.model.ExerciseInstructionsList.getExerciseList()
at com.rest.model.ExerciseInstructionsList this problem is related to the following location: at public java.util.List com.rest.model.ExerciseInstructionsList.exerciseList at com.rest.model.ExerciseInstructionsList ]
My Controller IS
#Controller
#RequestMapping("/")
public class ExerciseController {
#Autowired
private ExerciseService exerciseService;
private static final Logger logger = LoggerFactory.getLogger(ExerciseController.class);
#Consumes
#Produces
#RequestMapping(value=OaesRestURIConstants.GET_EXERCISE_ALL,method=RequestMethod.GET,produces={"application/json"})
public #ResponseBody List<ExerciseInstructions> getAllExercise()throws Exception{
logger.info("Start getAllExercises.");
System.out.println("<<<<<<<<<<<<<<<<<--------------Coming Inside List Exercise Controller----------->>>>>>>>>>>");
List<ExerciseInstructions> listExercise = new ArrayList<ExerciseInstructions>();
//ExerciseInstructionsList exe = new ExerciseInstructionsList();
/*This list contains Exercise Instructions Data*/
listExercise = exerciseService.getAllExercise();
/*here i kept the list in ExerciseInstructionsList list so that i can fetch xml data also and can show the list.*/
//exe.setExerciseList(listExercise);
return listExercise;
}
#RequestMapping(value=OaesRestURIConstants.GET_EXERCISE_XML_ALL,method=RequestMethod.GET,produces={"application/xml"})
public #ResponseBody ExerciseInstructionsList getAllXmlExercise()throws Exception{
logger.info("Start getAllExercises.");
System.out.println("<<<<<<<<<<<<<<<<<--------------Coming Inside List Exercise Controller----------->>>>>>>>>>>");
List<ExerciseInstructions> listExercise = new ArrayList<ExerciseInstructions>();
ExerciseInstructionsList exeList = new ExerciseInstructionsList();
/*This list contains Exercise Instructions Data*/
listExercise = exerciseService.getAllExercise();
/*here i kept the list in ExerciseInstructionsList list so that i can fetch xml data also and can show the list.*/
exeList.setExerciseList(listExercise);
return exeList;
}
#RequestMapping(value=OaesRestURIConstants.EXERCISE_SAVE,method=RequestMethod.POST)
public #ResponseBody ExerciseInstructions saveExercise(#RequestBody ExerciseInstructions exerciseInstructions)throws Exception{
logger.info("Start saveExercise.");
exerciseService.saveExercise(exerciseInstructions);
return exerciseInstructions;
}
//#Consumes({"application/xml","application/json"})
// #Produces({"application/xml","application/json"})
#RequestMapping(value=OaesRestURIConstants.GET_EXERCISE_ID,method=RequestMethod.GET,produces={"application/xml","application/json"})
public #ResponseBody ExerciseInstructions getExerciseById(#PathVariable("id") String exerciseId ) throws Exception{
logger.info("Start getExerciseById. ID="+exerciseId);
ExerciseInstructions exercise = null;
try {
exercise = exerciseService.getExerciseById(exerciseId);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Coming Here>>>>>>>>>>>"+exercise);
return exercise;
//return exerciseService.getExerciseById(exerciseId);
}
#RequestMapping(value=OaesRestURIConstants.EXERCISE_DELETE,method=RequestMethod.PUT)
public #ResponseBody ExerciseInstructions deleteById(#PathVariable("id") String exerciseId) throws Exception{
logger.info("Start deleteExercise.");
exerciseService.deleteExercise(exerciseId);
return null;
}
}
My Model class is :
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class ExerciseInstructions {}
My Model List Class Is :
#XmlRootElement(name="exerciseInstructions")
//#XmlSeeAlso({ExerciseInstructions.class})
#XmlAccessorType(XmlAccessType.FIELD)
public class ExerciseInstructionsList {
public List<ExerciseInstructions> exerciseList;
public List<ExerciseInstructions> getExerciseList() {
return exerciseList;
}
public void setExerciseList(List<ExerciseInstructions> exerciseList) {
this.exerciseList = exerciseList;
}
}
So can anyone help me in this.
I want to fetch and see both xml and json.

When you carefully read the error-message you see the reason: (I formatted and highlighted the message for better readability)
IllegalAnnotationExceptions Class has two properties of the same name "exerciseList"
this problem is related to the following location: at public java.util.List com.rest.model.ExerciseInstructionsList.getExerciseList() at com.rest.model.ExerciseInstructionsList
this problem is related to the following location: at public java.util.List com.rest.model.ExerciseInstructionsList.exerciseList at com.rest.model.ExerciseInstructionsList
So the program complains that your class ExerciseInstructionsList has two properties which can be mapped to exerciseList, these are getExerciseList() and exerciseList.
To fix this you can declare exerciseList as private.
#XmlRootElement(name="exerciseInstructions")
#XmlAccessorType(XmlAccessType.FIELD)
public class ExerciseInstructionsList {
private List<ExerciseInstructions> exerciseList;
public List<ExerciseInstructions> getExerciseList() {
return exerciseList;
}
public void setExerciseList(List<ExerciseInstructions> exerciseList) {
this.exerciseList = exerciseList;
}
}

Related

Get all documents from an index using spring-data-elasticsearch

I am trying to connect to my external ElasticSearch server with Spring Boot.
If I do a curl from command line, I get expected results.
curl "http://ipAddr:9200/indexName/TYPE/_search?pretty=true"
But getting this error when I try to access it via Spring Boot.
<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Mon Sep 11 12:39:15 IST 2017</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl["facets"])</div></body></html>
Not sure why a NullPointerException and what is aggregartion.impl
Here is my Spring Application:
Controller:
#RestController
public class PojoController {
#Autowired
PojoService pojoService;
#RequestMapping(value = "/", method=RequestMethod.GET)
public #ResponseBody String index() {
return new String("Welcome:)");
}
#RequestMapping(value = "/all", method = RequestMethod.GET,
produces = { MediaType.APPLICATION_JSON_VALUE })
#ResponseBody List<POJO> findAll() {
try {
List<POJO> pojoObj = pojoService.findAll();
return pojoObj;
} catch (Exception exp) {
exp.printStackTrace();
return null;
}
}
}
Repository:
#Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
List<POJO> findAll();
}
Service:
#Service
public class POJOServiceImpl implements POJOService{
private POJORepository pojoRepository;
private ElasticsearchTemplate elasticsearchTemplate;
#Autowired
public void setPojoRepository(PojoRepository pojoRepository) {
this.pojoRepository = pojoRepository;
}
public POJO findOne(String id) {
return pojoRepository.findOne(id);
}
public List<POJO> findAll() {
return (List<POJO>) pojoRepository.findAll();
}
}
POJO class:
#Document(indexName = "INDEX", type = "TYPE")
public class POJO {
#Id
private Integer id;
private String name;
public POJO(){
// empty
}
public POJO(Integerid, String name) {
super();
this.id = id;
this.name = name;
}
// getters and setters
}
I should be able to query all the documents in the index. Later on, I will try and use filters etc.
Any help is appreciated. Thanks :)
It looks like Jackson has a problem with handling your POJO (probably related to this issue: DATAES-274) - the problematic part is casting in repository from Iterable collection to List.
Update
In case of repositories, spring-data-elasticsearch behaves a bit different than you would expect. Taking your example:
#Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
List<POJO> findAll();
}
and after calling in your rest controller:
List<POJO> pojoObj = pojoService.findAll();
in debugger you will see something like this:
You would expect that pojoObj list contains objects of POJO class.
And here comes the surprise - pojoObj ArrayList contains one object of AggregatedPageImpl type and its content field is the right list that contains your POJO objects.
This is the reason why you get:
Could not write JSON: ... java.util.ArrayList[0]->org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl[\"facets\"])
As I wrote before, Jackson cannot handle this while serializing POJO objects.
Solution 1
Let repositories return Iterable collection (by default).
#Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
}
Move the conversion part to the service but use some utility method (here with Guava) in order to have it like this:
import com.google.common.collect.Lists;
public List<POJO> findAll() {
return Lists.newArrayList(pojoRepository.findAll());
}
Solution 2
Use Page in repository (here simplified version without parameters):
#Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
Page<TestDto> findAll();
}
If you still want to operate on list - get content from page in service:
public List<POJO> findAll() {
return testDtoRepository.findAll().getContent();
}

Java XML, The number of formal and actual parameters differs, or an unwrapping conversion has failed

When requesting data from endpoint with accept: application/xml I keep getting the following error:
javax.xml.bind.MarshalException
- with linked exception: [Exception [EclipseLink-27] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3):
org.eclipse.persistence.exceptions.DescriptorException Exception
Description: Trying to invoke the method [getSurveyid] on the object
[com.on24.ejb.mapping.SurveyQuestion]. The number of actual and
formal parameters differs, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: object is not
an instance of declaring class Mapping:
org.eclipse.persistence.oxm.mappings.XMLDirectMapping[surveyid-->surveyid/text()]
Descriptor: XMLDescriptor(com.on24.ejb.mapping.Survey -->
[DatabaseTable(survey)])]
The response works fine when accept: application/json so I know it can't be a problem extracting the info from DB; I just haven't been able to solve this issue so any help will be greatly appreciated.
DTOs involved:
#XmlRootElement
#XmlType (propOrder={"surveyid",
"surveyquestions"})
#XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Survey {
private Long surveyid;
private List<SurveyQuestion> surveyquestions;
public Survey(){}
public Long getSurveyid() {
return surveyid;
}
public void setSurveyid(Long surveyid) {
this.surveyid = surveyid;
}
#XmlElementWrapper(name="surveyquestionslist")
#XmlElement(name="surveyquestion")
public List<SurveyQuestion> getSurveyquestions() {
return surveyquestions;
}
public void setSurveyquestions(List<SurveyQuestion> surveyquestions) {
this.surveyquestions = surveyquestions;
}
}
And
#XmlRootElement
#XmlType (propOrder={"surveyquestionid",
"surveyquestion",
"surveyanswers"})
#XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class SurveyQuestion {
private Long surveyquestionid;
private String surveyquestion;
private List<String> surveyanswers;
public SurveyQuestion(){}
public Long getSurveyquestionid() {
return surveyquestionid;
}
public void setSurveyquestionid(Long surveyquestionid) {
this.surveyquestionid = surveyquestionid;
}
public String getSurveyquestion() {
return surveyquestion;
}
public void setSurveyquestion(String surveyquestion) {
this.surveyquestion = surveyquestion;
}
#XmlElementWrapper(name="surveyanswerslist")
#XmlElement(name="surveyanswer")
public List<String> getSurveyanswers() {
return surveyanswers;
}
public void setSurveyanswers(List<String> surveyanswers) {
this.surveyanswers = surveyanswers;
}
}
I've tried several thinks from refactoring to use XmlAccessType.PUBLIC_MEMBER, XmlAccessType.FIELD, XmlAccessType.PROPERTY but no success there.
I'd really like to understand why this error is generated. If more info is need I'll add it as per asked for, thanks.

Spring Validation rejectValue for inherited fields

I get Exception
org.springframework.beans.NotReadablePropertyException: Invalid property 'entries[0].reason' of bean class [my.company.data.SDROrder]: Bean property 'entries[0].reason' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
from the following code snippet:
Errors errors = new BeanPropertyBindingResult(new SDROrder(), "sdr");
orderValidator.validate(order, errors);
for validator:
public class OrderValidator implements Validator
{
#Override
public boolean supports(Class<?> clazz)
{
return Order.class.isAssignableFrom(clazz);
}
#Override
public void validate(final Object target, final Errors errors)
{
errors.rejectValue("entries[0].reason", "Wrong Reason");
}
}
where we have such data hierarchy
public class Order
{
private List<AbstractOrderEntry> entries;
public List<AbstractOrderEntry> getEntries()
{
return entries;
}
public void setEntries(List<AbstractOrderEntry> entries)
{
this.entries = entries;
}
}
public class SDROrder extends Order
{
}
public class AbstractOrderEntry
{
}
public class SDROrderEntry extends AbstractOrderEntry
{
private String reason;
public String getReason()
{
return reason;
}
public void setReason(String reason)
{
this.reason = reason;
}
}
Please see working example here: here
Update 1: Just to clarify. The problem is I try to rejectValue on object that has Collection of objects where each element has specific attribute at Runtime but has not it at Compile time. Spring uses Bean's properties to resolve these fields and can't find inherited attribute. The question is: can I explain Spring to resolve inherited fields somehow?
I found the solution here.
The trick is at
org.springframework.validation.Errors.pushNestedPath(String)
and
org.springframework.validation.Errors.popNestedPath()
methods.
The correct validation should be done as follow:
errors.pushNestedPath("entries[0]");
errors.rejectValue("reason", "Wrong Reason");
errors.popNestedPath();

How to validate Spring MVC #PathVariable values?

For a simple RESTful JSON api implemented in Spring MVC, can I use Bean Validation (JSR-303) to validate the path variables passed into the handler method?
For example:
#RequestMapping(value = "/number/{customerNumber}")
#ResponseBody
public ResponseObject searchByNumber(#PathVariable("customerNumber") String customerNumber) {
...
}
Here, I need to validate the customerNumber variables's length using Bean validation. Is this possible with Spring MVC v3.x.x? If not, what's the best approach for this type of validations?
Thanks.
Spring does not support #javax.validation.Valid on #PathVariable annotated parameters in handler methods. There was an Improvement request, but it is still unresolved.
Your best bet is to just do your custom validation in the handler method body or consider using org.springframework.validation.annotation.Validated as suggested in other answers.
You can use like this:
use org.springframework.validation.annotation.Validated to valid RequestParam or PathVariable.
*
* Variant of JSR-303's {#link javax.validation.Valid}, supporting the
* specification of validation groups. Designed for convenient use with
* Spring's JSR-303 support but not JSR-303 specific.
*
step.1 init ValidationConfig
#Configuration
public class ValidationConfig {
#Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
return processor;
}
}
step.2 Add #Validated to your controller handler class, Like:
#RequestMapping(value = "poo/foo")
#Validated
public class FooController {
...
}
step.3 Add validators to your handler method:
#RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public ResponseEntity<Foo> delete(
#PathVariable("id") #Size(min = 1) #CustomerValidator int id) throws RestException {
// do something
return new ResponseEntity(HttpStatus.OK);
}
final step. Add exception resolver to your context:
#Component
public class BindExceptionResolver implements HandlerExceptionResolver {
#Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
if (ex.getClass().equals(BindException.class)) {
BindException exception = (BindException) ex;
List<FieldError> fieldErrors = exception.getFieldErrors();
return new ModelAndView(new MappingJackson2JsonView(), buildErrorModel(request, response, fieldErrors));
}
}
}
The solution is simple:
#GetMapping(value = {"/", "/{hash:[a-fA-F0-9]{40}}"})
public String request(#PathVariable(value = "hash", required = false) String historyHash)
{
// Accepted requests: either "/" or "/{40 character long hash}"
}
And yes, PathVariables are ment to be validated, like any user input.
Instead of using #PathVariable, you can take advantage of Spring MVC ability to map path variables into a bean:
#RestController
#RequestMapping("/user")
public class UserController {
#GetMapping("/{id}")
public void get(#Valid GetDto dto) {
// dto.getId() is the path variable
}
}
And the bean contains the actual validation rules:
#Data
public class GetDto {
#Min(1) #Max(99)
private long id;
}
Make sure that your path variables ({id}) correspond to the bean fields (id);
#PathVariable is not meant to be validated in order to send back a readable message to the user. As principle a pathVariable should never be invalid. If a pathVariable is invalid the reason can be:
a bug generated a bad url (an href in jsp for example). No #Valid is
needed and no message is needed, just fix the code;
"the user" is manipulating the url.
Again, no #Valid is needed, no meaningful message to the user should
be given.
In both cases just leave an exception bubble up until it is catched by
the usual Spring ExceptionHandlers in order to generate a nice
error page or a meaningful json response indicating the error. In
order to get this result you can do some validation using custom editors.
Create a CustomerNumber class, possibly as immutable (implementing a CharSequence is not needed but allows you to use it basically as if it were a String)
public class CustomerNumber implements CharSequence {
private String customerNumber;
public CustomerNumber(String customerNumber) {
this.customerNumber = customerNumber;
}
#Override
public String toString() {
return customerNumber == null ? null : customerNumber.toString();
}
#Override
public int length() {
return customerNumber.length();
}
#Override
public char charAt(int index) {
return customerNumber.charAt(index);
}
#Override
public CharSequence subSequence(int start, int end) {
return customerNumber.subSequence(start, end);
}
#Override
public boolean equals(Object obj) {
return customerNumber.equals(obj);
}
#Override
public int hashCode() {
return customerNumber.hashCode();
}
}
Create an editor implementing your validation logic (in this case no whitespaces and fixed length, just as an example)
public class CustomerNumberEditor extends PropertyEditorSupport {
#Override
public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.hasText(text) && !StringUtils.containsWhitespace(text) && text.length() == YOUR_LENGTH) {
setValue(new CustomerNumber(text));
} else {
throw new IllegalArgumentException();
// you could also subclass and throw IllegalArgumentException
// in order to manage a more detailed error message
}
}
#Override
public String getAsText() {
return ((CustomerNumber) this.getValue()).toString();
}
}
Register the editor in the Controller
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(CustomerNumber.class, new CustomerNumberEditor());
// ... other editors
}
Change the signature of your controller method accepting CustomerNumber instead of String (whatever your ResponseObject is ...)
#RequestMapping(value = "/number/{customerNumber}")
#ResponseBody
public ResponseObject searchByNumber(#PathVariable("customerNumber") CustomerNumber customerNumber) {
...
}
You can create the answer you want by using the fields in the ConstraintViolationException with the following method;
#ExceptionHandler(ConstraintViolationException.class)
protected ResponseEntity<Object> handlePathVariableError(final ConstraintViolationException exception) {
log.error(exception.getMessage(), exception);
final List<SisSubError> subErrors = new ArrayList<>();
exception.getConstraintViolations().forEach(constraintViolation -> subErrors.add(generateSubError(constraintViolation)));
final SisError error = generateErrorWithSubErrors(VALIDATION_ERROR, HttpStatus.BAD_REQUEST, subErrors);
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
You need to added an #Validated annotation to Controller class and any validation annotation before path variable field
Path variable may not be linked with any bean in your system. What do you want to annotate with JSR-303 annotations?
To validate path variable you should use this approach Problem validating #PathVariable url on spring 3 mvc
Actually there is a very simple solution to this. Add or override the same controller method with its request mapping not having the placeholder for the path variable and throw ResponseStatusException from it. Code given below
#RequestMapping(value = "/number")
#ResponseBody
public ResponseObject searchByNumber() {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"customer number missing")
}

JAXB Error while using in SpringREST to return a ArrayList of a domain object

I am trying to use JAXB in Spring RESTful webservice.
My code is as follows:
#RequestMapping(value = "/countries",
method = RequestMethod.GET,
headers="Accept=application/xml, application/json")
public #ResponseBody CountryList getCountry() {
logger.debug("Provider has received request to get all persons");
// Call service here
CountryList result = new CountryList();
result.setData(countryService.getAll());
return result;
}
The CountryList.java class looks like:
#XmlRootElement(name="countries")
public class CountryList {
#XmlElement(required = true)
public List<Country> data;
#XmlElement(required = false)
public List<Country> getData() {
return data;
}
public void setData(List<Country> data) {
this.data = data;
}
}
The Country.java looks like:
#XmlRootElement(name="country")
public class Country {
private Calendar createdDt;
private String updatedBy;
private String createdBy;
private Long id;
private String countryName;
private Calendar updatedDt;
// getters and setters for all attributes goes here
}
Now, when I access the method getCountry(), I am getting the following exception
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "data"
this problem is related to the following location:
at public java.util.List com.cisco.bic.services.model.CountryList.getData()
at com.cisco.bic.services.model.CountryList
this problem is related to the following location:
at public java.util.List com.cisco.bic.services.model.CountryList.data
at com.cisco.bic.services.model.CountryList
Would anyone has any idea why is this error coming. Am I doing anything wrong in the annotaion part ??
Please help.
Regards
Saroj
You can't annotate both the getter/setter and the field, you need to decide on one of them.

Resources