unable to pass object as method parameter - java

I am trying to pass a person object to my test method below but i am getting an error when i try to run it in postman. Any help will be greatly appreciated
In postman, i used the following JSON in the body
{
"name" : "aasd",
"address" : "asd",
"age" : "asdads",
"sex" : "asdsad"
}
Person model
package com.test.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.math.BigDecimal;
import java.util.Date;
#XmlRootElement
public class Person{
#XmlElement
private String name;
#XmlElement
private String address;
public Person() {
}
public void setName(String name) {
this.name= name;
}
#XmlElement
public String getName() {
return name;
}
public void setAddress(String address) {
this.address= address;
}
#XmlElement
public String getAddress() {
return address;
}
}
Controller
package com.test2.controller;
import javax.annotation.security.PermitAll;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.test.model;
#Path("person")
public class PersonController {
#PermitAll
#POST
#Produces({MediaType.APPLICATION_JSON})
#Consumes({MediaType.APPLICATION_JSON})
#Path("test")
public Person addPerson(Person person) {
return person;
}
}
Error Message
exception
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
root cause
org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
root cause
java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper

Related

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectTaskRepository'

I am doing project on spring boot...but got this error while running the application. It might be the H2 database error. But to resolve the error localhost should run but program terminates before it being deployed..so i could not able to see what is the actual problem.
Error: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectTaskRepository' defined in com.agileintelligence.projectdashboard.repository.ProjectTaskRepository defined in #EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.agileintelligence.projectdashboard.domain.projectTask
Table class
package com.agileintelligence.projectdashboard.domain;
import org.springframework.data.annotation.Id;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.validation.constraints.NotBlank;
#Entity
public class projectTask {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#NotBlank(message = "Summary cannot be empty")
private String summary;
private String acceptanceCriteria;
private String status;
public projectTask() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getAcceptanceCriteria() {
return acceptanceCriteria;
}
public void setAcceptanceCriteria(String acceptanceCriteria) {
this.acceptanceCriteria = acceptanceCriteria;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Controller
package com.agileintelligence.projectdashboard.web;
import com.agileintelligence.projectdashboard.domain.projectTask;
import com.agileintelligence.projectdashboard.service.ProjectTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
#RestController
#RequestMapping("/api/board")
#CrossOrigin
public class ProjectTaskController {
#Autowired
private ProjectTaskService service;
#PostMapping("")
public ResponseEntity<?> addProjectTaskToBoard(#RequestBody projectTask projectTask){
com.agileintelligence.projectdashboard.domain.projectTask newPT = service.saveOrUpdateProjectTask(projectTask);
return new ResponseEntity<>(newPT, HttpStatus.CREATED);
}
}
Service Class
package com.agileintelligence.projectdashboard.service;
import com.agileintelligence.projectdashboard.domain.projectTask;
import com.agileintelligence.projectdashboard.repository.ProjectTaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class ProjectTaskService {
#Autowired
private ProjectTaskRepository projectTaskRepository;
public projectTask saveOrUpdateProjectTask(projectTask projectTask){
//In case if status is not set,automatically push it to TO_DO
if(projectTask.getStatus()==null||projectTask.getStatus().equals("")){
projectTask.setStatus("TO_DO");
}
return projectTaskRepository.save(projectTask);
}
}
Repository
package com.agileintelligence.projectdashboard.repository;
import com.agileintelligence.projectdashboard.domain.projectTask;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface ProjectTaskRepository extends CrudRepository<projectTask,Long> {
}
You are using the wrong import for the Id here.
import org.springframework.data.annotation.Id;
Replace the above one with below from persistence package.
import javax.persistence.Id
Bonus : Rename your entity with ProjectTask (P uppercase) to prevent future strange issues.

Parameter 0 of constructor in com.example.demo1.service.PersonService required a bean of type 'com.example.demo1.dao.PersonDao'

I am new to Spring Boot Framework without any prior knowledge of the Spring Framework. I encountered this error when I am running the spring application:
The error points to the service class
PersonService.java
package com.example.demo1.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.example.demo1.dao.PersonDao;
import com.example.demo1.model.Person;
#Service
public class PersonService {
private final PersonDao personDao;
// The #Autowired means that we are injecting in actual constructor. It means we are autowiring in the PersonDao interface
// We have multiple implementation of the PersonDao interface. So to distinguish between them we use the #Qualifier
#Autowired
public PersonService(#Qualifier("fake") PersonDao personDao) {
this.personDao = personDao;
}
// Here we have the option of providing the id or not
public int addPerson(Person person) {
return personDao.insertPerson(person);
}
}
The error shows that the bean of the type PersonDao is required that could not be found. But I could not identify how to create the bean. I have used the dependency injection for the PersonDao class.
PersonDao.java
package com.example.demo1.dao;
import java.util.UUID;
import com.example.demo1.model.Person;
public interface PersonDao {
int insertPerson(UUID id, Person person);
default int insertPerson(Person person) {
UUID id = UUID.randomUUID();
return insertPerson(id, person);
}
}
FakePersonDataAccessService
package com.example.demo1.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import com.example.demo1.model.Person;
// The #Repository annotation means that this class is served as a Repository
#Repository("fakeDao")
public class FakePersonDataAccessService implements PersonDao {
private static List<Person> DB = new ArrayList<>();
#Override
public int insertPerson(UUID id, Person person) {
DB.add(new Person(id, person.getName()));
return 1;
}
}
Person.java
package com.example.demo1.model;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person {
private final UUID id;
private final String name;
public Person(#JsonProperty("id") UUID id,
#JsonProperty("name") String name) {
this.id = id;
this.name = name;
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
}
PersonController.java
package com.example.demo1.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo1.model.Person;
import com.example.demo1.service.PersonService;
// We can implement the http methods(get, put, post, delete) implementation in the controller. We can do that by using the #RestController annotation
#RequestMapping("/api/v1/person")
#RestController
public class PersonController {
private final PersonService personService;
// The #Autowired means that the spring boot injects actual service in the constructor
#Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}
// The #RequestBody annotation shows that we convert the json body that we receive from the postman to an actual Person
#PostMapping
public void addPerson(#RequestBody Person person) {
personService.addPerson(person);
}
}

Building controller using Spring RestController and Jackson give me HTTP Stats 406

I'm building a rest controller using Spring to handle request and Jackson to serialize data.However I followed tutorial online but I end up getting an error.
HTTP Status 406 -
type Status report
message
description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
After Google for a while, I realized that it is because I don't have "application/json" as my "Accept" header in my request:
So I use a tool called Postman to manually add this "Accept" header in the request, send the request again, but still getting the same error:
I'm so confused, I've already included "application/json" as one of accepted data-type, why I still have this data-unsupported error? FYI, here is my Rest Controller class:
package mywebapp.controller;
import java.io.IOException;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import mywebapp.dao.model.interfaces.PetDao;
import mywebapp.model.Pet;
#RestController
#RequestMapping(value = "petJson.htm")
public class PetControllerAjax {
private static final Logger LOG = LoggerFactory.getLogger(PetController.class);
public static Logger getLog() {
return LOG;
}
#Autowired
#Qualifier("PetDaoJpaImpl")
private PetDao petDao;
public PetDao getPetDao() {
return petDao;
}
public void setPetDao(PetDao petDao) {
this.petDao = petDao;
}
#RequestMapping(method = RequestMethod.GET)
public List<Pet> getAllPets() throws IOException {
getLog().info("Rest Controller activating........");
List<Pet> petList = getPetDao().getAllPets();
return petList;
}
}
And here is my Pet entity class:
package mywebapp.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
import java.util.Set;
#Entity
#Table(name = "pet")
public class Pet {
private int petId;
private String name;
private String owner;
private String species;
private String sex;
private Date birth;
private Date death;
private Set<Toy> toys;
#Id
#Column(name = "pet_id")
#GeneratedValue
#JsonProperty(value="pet_id",required=true)
public int getId() {
return petId;
}
public void setId(int id) {
this.petId = id;
}
#JsonProperty(value="pet_name",required=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Date getDeath() {
return death;
}
public void setDeath(Date death) {
this.death = death;
}
#OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,targetEntity=Toy.class, mappedBy="pet")
public Set<Toy> getToys() {
return toys;
}
public void setToys(Set<Toy> toys) {
this.toys = toys;
}
}
Anyone knows what's going on here? Any hint will be appreciated, lots of thanks in advance!
Jackson 2.7 is not supported by Spring 4.2 - it will be in 4.3+.
Check out the library requirements for Spring on the Spring wiki and see SPR-13728 and SPR-13483.

Why this REST service is not working?

package model;
import java.net.URI;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
#Path("/item")
#Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
#Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
#Stateless
public class InfoRestService {
// the PersistenceContext annotation is a shortcut that hides the fact
// that, an entity manager is always obtained from an EntityManagerFactory.
// The peristitence.xml file defines persistence units which is supplied by
// name
// to the EntityManagerFactory, thus dictating settings and classes used by
// the
// entity manager
#PersistenceContext(unitName = "Task")
private EntityManager em;
// Inject UriInfo to build the uri used in the POST response
#Context
private UriInfo uriInfo;
#POST
public Response createItem(PersonInfo item) {
if (item == null) {
throw new BadRequestException();
}
em.persist(item);
// Build a uri with the Item id appended to the absolute path
// This is so the client gets the Item id and also has the path to the
// resource created
URI itemUri = uriInfo.getAbsolutePathBuilder().path(item.getId()).build();
// The created response will not have a body. The itemUri will be in the
// Header
return Response.created(itemUri).build();
}
#GET
#Path("{id}")
public Response getItem(#PathParam("id") String id) {
PersonInfo item = em.find(PersonInfo.class, id);
if (item == null) {
throw new NotFoundException();
}
return Response.ok(item).build();
}
// Response.ok() does not accept collections
// But we return a collection and JAX-RS will generate header 200 OK and
// will handle converting the collection to xml or json as the body
#GET
public Collection<PersonInfo> getItems() {
TypedQuery<PersonInfo> query = em.createNamedQuery("PersonInfo.findAll",
PersonInfo.class);
return query.getResultList();
}
#PUT
#Path("{id}")
public Response updateItem(PersonInfo item, #PathParam("id") String id) {
if (id == null) {
throw new BadRequestException();
}
// Ideally we should check the id is a valid UUID. Not implementing for
// now
item.setId(id);
em.merge(item);
return Response.ok().build();
}
#DELETE
#Path("{id}")
public Response deleteItem(#PathParam("id") String id) {
PersonInfo item = em.find(PersonInfo.class, id);
if (item == null) {
throw new NotFoundException();
}
em.remove(item);
return Response.noContent().build();
}
}
package model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
/**
* The persistent class for the person_info database table.
*
*/
#Entity
#XmlRootElement
#Table(name="person_info")
#NamedQuery(name="PersonInfo.findAll", query="SELECT p FROM PersonInfo p")
public class PersonInfo implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private String id;
private String email;
#Column(name="first_name")
private String firstName;
#Column(name="last_name")
private String lastName;
public PersonInfo() {
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Task">
<jta-data-source>jdbc/DBtest</jta-data-source>
<class>model.PersonInfo</class>
</persistence-unit>
</persistence>
and the other class is Application
package model;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("rest")
public class ApplicationConfig extends Application{
}
I really have no idea, the connections are made ok .. I'm using Glassfish 4 server and MySQL database... code is deploying but when I want to access the localhost:8080/Task/.. (my app) the only thing it says is this:
"HTTP Status 404 - Not Found / Type Status report
messageNot Found
descriptionThe requested resource is not available."
The code you supplied is working (when commenting out the persistence related stuff), I guess you are just confusing something.
The #ApplicationPath annotation sets the root context which comes after your project name.
If you project name really is Task you have to use this URL: http://localhost:8080/Task/rest/item
Otherwise: http://localhost:8080/YOUR_PROJECT_NAME/rest/item
See also:
How to set up JAX-RS Application using annotations only (no web.xml)?

Jersey - Rest API (Json Format)

I am trying to create an Jersey application and I would like to pass the output to web browser in Json format. Below are the codes. I can only pass the data in xml format and when I change to "application/json" format, "HTTP Status 500 - Internal Server Error" came out. Any advice? thanks.
import java.net.URISyntaxException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
#Path("/emp")
public class EmployeeService {
#GET
#Path("/get/{empID}")
#Produces({"application/json"})
public Employee getEmployee(#PathParam("empID") String empID){
Employee employee = new com.rest.employee.model.Employee();
employee.setEmpID(empID);
employee.setName("George");
employee.setEmail("george#gmail.com");
return employee;
}
}
and another java file
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlAccessorType(XmlAccessType.NONE)
#XmlRootElement(name="employee")
public class Employee {
public String empID;
public String name;
public String email;
#XmlElement(required=true)
public String getEmpID() {
return empID;
}
public void setEmpID(String empID) {
this.empID = empID;
}
#XmlElement(required=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#XmlElement(required=true)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
The code looks 'nearly' fine and works for me as expected, but your Employee should implement Serializable. I can't see any other problem in there for now, so the problem is somewhere else i guess.
You said, that its not working anymore, since you changed from XML to JSON. So i reckon, that you miss a dependency for JSON. Check this ...
But in fact, the missing dependency should not result in an 500. Can it be true, that you have added a ExceptionMapper and that you throw your 500 by yourself?
You need to integrate Jersey with Jackson. Look at this tutorial:
http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/

Categories

Resources