I used swagger in my apache cxf project , used #Api and #ApiOperations and #ApiParam annotations and generated a api doc for the rest services.
But I want to exclude some of the fields like EntityTag, StatusType and MediaType etc from Models attribute or complete modules or properties attribute.
How to do that?
I was fetching data from db and setting it to user object and passing that user object to JAX-RS response builder.
Below is one of my DTO Object:
#ApiModel
public class User{
private String name;
private String email;
#ApiModelProperty(position = 1, required = true, notes = "used to display user name")
public int getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
#ApiModelProperty(position = 2, required = true, notes = "used to display user email")
public int getEmail() {
return email;
}
public void setEmail(String email) {
this.email= email;
}
Now I don't see the User object fields or properties inside the Swagger returned json format.
my service class method response is :
#GET
#ApiOperation(value = "xxx", httpMethod = "GET", notes = "user details", response = Response.class)
public Response getUserInfo(){
User userdto = userdaoimpl.getUserDetails();
ResponseBuilder builder = Response.ok(user, Status.OK), MediaType.APPLICATION_JSON);
builder.build();
}
<bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
<property name="resourcePackage" value="com.services.impl" />
<property name="version" value="1.0.0" />
<property name="basePath" value="http://localhost:8080/api" />
<property name="license" value="Apache 2.0 License" />
<property name="licenseUrl"
value="http://www.apache.org/licenses/LICENSE-2.0.html" />
<property name="scan" value="true" />
</bean>
First of all, you should upgrade to the latest swagger-core version, currently 1.3.12 (you're using a really old one).
You have 3 ways to hide a property:
If you're using JAXB annotations, you can use #XmlTransient.
If you're using Jackson, you can use #JsonIgnore.
If you're not using either, or don't want to affect the general de/serialization of your models, you can use Swagger's #ApiModelProperty's hidden attribute.
Keep in mind you may need to set these on your getters/setters rather than on the property itself. Play with the definitions to see what works for you.
With regards to the issue with the User model, the problem is that you do not reference it from the #ApiOperation (you also don't need the httpMethod property). Try changing it as follows:
#ApiOperation(value = "xxx", notes = "user details", response = User.class)
You can exclude fields like that :
#ApiModelProperty(position = 1, required = true, hidden=true, notes = "used to display user name")
Related
#GetMapping("/getAccount")
public Account validateAccount(#RequestBody) {
}
Very new to spring boot. My account file has 5+ values all strings, username, password, id, and some etc things.
Given this
{
"username": "bob"
"password": "password"
}
It should give this with 200 response code OK
{
"id": "45645646546"
"username": "bob"
"password": "password"
"status": "Single"
"filler": "filler"
}
However I'm not sure how to read the "username" and "password" json in my validateAccount function
Not really related to this question but does anyone know how to send a response code in the function? Like .sendresponseheader(400) something like that
public class AccountDTO {
#JsonIgnore
private Long id;
#NotNull
private String username;
#NotNull
private String password;
#JsonIgnore
private String status;
#JsonIgnore
private String filler;
// getters & setters
}
You may want to create a DTO (Data Transaction Object) as shown above. Here's a link to it's wiki.
Next pass map user input into this DTO using #RequestBody annotation.
#RestController
public class AccountController {
#GetMapping("/accounts")
public ResponseEntity<Account> validateAccount(#RequestBody AccountDTO accountDTO) {
return new ResponseEntity<>(accountService.validate(accountDTO), HttpStatus.OK);
}
}
Or you can use
#RestController
public class AccountController {
#GetMapping("/accounts")
public Response validateAccount(#RequestBody AccountDTO accountDTO) {
return new ResponseEntity().ok(accountService.validate(accountDTO));
}
}
The user input will be converted from json to AccountDTO using whatever JSON processor your're using most probably it'll be com.fasterxml.jackson.core.
The #JsonIgnore and #NotNull annotation will ensure only username and password fields are used and others are ignored while taking input from user.
You can pass this DTO to your service classes and use something like findByUsername() in your Business Logic and return populated AccountDTO using the below mapper function or some external libraries like Model Mapper or MapStruct.
public toAccountDTO(Account account) {
AccountDTO accountDTO = new AccountDTO();
accountDTO.setUsername(account.getUsername());
// and so on...
return accountDTO;
}
And for your last query, wrap the returned AccountDTO object in ResponseEntity wrapper to provide a proper Response Code with your payload. Here's a link to ResponseEntity Java docs.
AccountDto.java
===============
class AccountDto{
private Long id;
private String username;
private String password;
private String status;
private String filler;
//getters & setters
}
#GetMapping("/getAccount")
public ResponseEntity validateAccount(#RequestBody AccountDto accountDto) {
return new ResponseEntity<>(accountServie.validate(accountDto),HttpStatus.OK);
}
You can do your custom operations before returning the response. Take a look Best Practice of REST
For json response nothing specific just mark class with #RestController.
For #RequestBody just use a pojo to bind the values
For error code and status you can use ResponseEntity
I have a Java class representing a customer with some properties like name, adress etc.
And I have the property:
#Id
#Property("_id")
private ObjectId id;
The customer will be fetched from a mongoDB. Everything is fine and all properties are filled.
Then I try to transport data via REST to a angular2 client
I have a Customer representation in typescript
export class Customer {
public id: string <---
Mapping inside rest call in client
.map((response: Response) => <Customer> response.json())
what do I need to put here, that the property will be mapped and that I can use it within the angular client.
I've tried to install bson-objectid via npm, but I have no idea how to get it to map the id property. All the others are working fine.
Solution found!
Create a
public YourAdapterName extends XMLAdapter<String, ObjectID> {
#Override
public String marshal(ObjectId v) throws Exception {
return v.toString();
}
#Override
public ObjectId unmarshal(String v) throws Exception {
return new ObjectId(v);
}
}
This Adapter returns the string representation of the ObjectID and I can use
id: string
in Typescript.
I am trying to make a RESTful application in Java using Spring boot by following the tutorial here. I want to modify it so that I can extract an identifier from the URL and use it to serve requests.
So http://localhost:8080/members/<memberId> should serve me a JSON object with information about the member whose ID is <memberId>. I don't know how to
Map all http://localhost:8080/members/* to a single controller.
Extract the from the URL.
Should the logic of extracting the memberId and using it be part of the controller or a separate class, as per the MVC architecture?
I am new to Spring/Spring-boot/MVC. It is quite confusing to get started with. So please bear with my newbie questions.
Map all http://localhost:8080/members/* to a single controller.
You can use a placeholder in a request mapping to so it'll handle multiple URLs. For example:
#RequestMapping("/members/{id}")
Extract the id from the URL
You can have the value of a placeholder injected into your controller method using the #PathVariable annotation with a value that matches the name of the placeholder, "id" in this case:
#RequestMapping("/members/{id}")
public Member getMember(#PathVariable("id") long id) {
// Look up and return the member with the matching id
}
Should the logic of extracting the memberId and using it be part of the controller or a separate class, as per the MVC architecture?
You should let Spring MVC extract the member id from the URL as shown above. As for using it, you'll probably pass the URL to some sort of repository or service class that offers a findById method.
As you can see in the code below, service for customer are in one controller to get one and to add new customer.
So, you will have 2 services:
http://localhost:8080/customer/
http://localhost:8080/customer/{id}
#RestController("customer")
public class SampleController {
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Customer greetings(#PathVariable("id") Long id) {
Customer customer = new Customer();
customer.setName("Eddu");
customer.setLastname("Melendez");
return customer;
}
#RequestMapping(value = "/{id}", method = RequestMethod.POST)
public void add(#RequestBody Customer customer) {
}
class Customer implements Serializable {
private String name;
private String lastname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getLastname() {
return lastname;
}
}
}
I am new to using Play! Framework 2.X and have a simple question.
How can i send to my method Job.create(filledJob) a fully set Job object? Meaning that Company object including in Job object is filled too.
I have the following:
public class Job {
public String title;
public Company company;
}
public class Company {
public String name;
public String email;
}
I have a form in my template, containing a Job information section and a Company section
#(jobForm: Form[Job], companyForm: Form[Company])
#form(routes.Application.newJob()) {
#inputText(jobForm("title"))
#inputText(companyForm("name"))
<input type="submit" value="Create">
}
My controller(Application.java) looks like that:
public class Application extends Controller {
static Form<Job> jobForm = form(Job.class);
static Form<Company> companyForm = form(Company.class);
public static Result index() {
return ok(
views.html.index.render(jobForm, companyForm)
);
}
public static Result newJob() {
Form<Job> filledForm = jobForm.bindFromRequest();
Job.create(filledForm.get());
return redirect(routes.Application.index());
}
}
Ideally, i would like to send to my method Job.create(job), with job with all the fields set (string job title as well as Company object). It must be a numpty question, i appologize for that. Any help would be greatly appreciated
Many thanks
You can't use #inputText(companyForm("name")) if you want bind the form directly into object, but instead you can use a select form helper for selecting the company, check ie. computer-database sample it's quite similar case...
On the other hand if you have a lot of companies and don't want to create huge select consider another scenario: just instead route like /job/new use /company/:companyId/add-job in other words just you'll send a form and bind it from request + additionaly will get Company by id in route param and will add to object, pseudo code:
public static Result addJobToCompany(Integer companyId){
Job job = form(Job.class).bindFromRequest().get();
job.company = Company.find.byId(companyId);
job.save();
return redirect(routes.Application.jobDetails(job.id));
}
Ok i am replying to my own question as i must not have explained my question clearly.
To make it simple : I needed to set the Company object from a form. Find below my solution
Hope it will help you.
public class Job {
public String title;
#Valid
public Company company;
}
public class Company {
public String name;
public String email;
}
#(jobForm: Form[Job], companyForm: Form[Company])
#form(routes.Application.newJob()) {
#inputText(jobForm("title"))
#inputText(jobForm("company.name"))
<input type="submit" value="Create">
}
Good evening everybody, this is my first post on Stack Overflow.
I have been quite recently introduced to Java 6 EE and, in particular, to JPA as part of the JSF 2.1 framework and I am now facing a strange behavior that I would like you to help me understand.
In our project (developed using NetBeans 7.2) we have several one-to-many relationship and we would like to navigate them the same way we navigate many-to-one ones. The fact is that, instead, we are able to make them work as we want only after having restarted the application server (Glassfish 3.1.2) and, in addition, this behavior lasts only till the next deployment; which means we need to restart Glassfish every time we apply a modification...
Here are some code excerpts to help you understand our situation.
This represents our main entity (Person) that has, among the others, a one-to-many relationship with Email as well as with Phone and a many-to-one relationship with AccountType
#Entity
public class Person implements Serializable {
//
//private non-collection fields including id
//
#OneToMany(mappedBy="person", fetch=FetchType.EAGER)
private Collection<Email> personEmails;
#OneToMany(mappedBy="person")
private Collection<Phone> personPhones;
#ManyToOne
private AccountType accountType;
//
// getter and setter, hashCode, isEqual and toString
//
}
And these are Email...
#Entity
public class Email implements Serializable {
//
//private non-collection fields including id
//
private String address;
#ManyToOne
private Person person;
//
// getter and setter, hashCode, isEqual and toString
//
}
... Phone ...
#Entity
public class Phone implements Serializable {
//
//private non-collection fields including id
//
private String number;
#ManyToOne
private Person person;
//
// getter and setter, hashCode, isEqual and toString
//
}
... and AccountType
#Entity
public class AccounType implements Serializable {
//
//private non-collection fields including id
//
private String name;
#OneToMany(mappedBy="accountType")
private Collection<Person> persons;
//
// getter and setter, hashCode, isEqual and toString
//
}
We have then set up a sample page to test how that three fields in Person are actually fetched.
This represents the xhtml page...
<h:form id="form">
<h:panelGrid columns="2">
<h:outputLabel value="forename" />
<h:outputLabel value="#{meBean.currentUser.forename}" />
<h:outputLabel value="emails" />
<h:outputLabel value="#{meBean.currentUser.personEmails.size()}" />
<h:outputLabel value="phones" />
<h:outputLabel value="#{meBean.currentUser.personPhones}" />
<h:outputLabel value="accountType" />
<h:outputLabel value="#{meBean.currentUser.accountType.name}" />
</h:panelGrid>
</h:form>
... and this the controller
#ManagedBean
#RequestScoped
public class MeBean {
#EJB
private PersonFacade personFacade;
private Person currentUser;
public MeBean() {
init();
}
#PostConstruct
private void init() {
// Hard-coding user details
try {
this.currentUser = this.personFacade.getFromUsername("user1");
this.currentUser.getPersonPhones().isEmpty();
} catch (Exception e) {
}
}
public Person getCurrentUser() {
return currentUser;
}
public void setCurrentUser(Person currentUser) {
this.currentUser = currentUser;
}
}
Now, the result we get is the one we expect only if we access the page right after having restarted the application server.
forename Alice
emails 2
phones {[sums.groupa.entities.Phone[id=38]]}
accountType Student
If we modify anything (except for the view) and save, after the inevitable deploy, the result is different.
forename Alice
emails 0
phones {[]}
accountType Student
Why is that happening and how can we avoid it?
Thanks in advance.
AG
A couple of contributors (that I want to thank for their quick replies) asked for the PersonFacade implementation.
public Person getFromUsername(String username)
{
try
{
Query q = em.createQuery("SELECT p FROM Person p LEFT JOIN FETCH p.personEmails WHERE UPPER(p.username) = :username");
q.setParameter("username", username.toUpperCase());
return (Person) q.getSingleResult();
}
catch (Exception ex)
{
Logger.getLogger(PersonFacade.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
As you can see I tried to use FETCH JOIN as suggested but the query is getting out too many results: it should fetch only one instance of Person representing Alice and containing two instances of Email in the personEmails field but I suspect it is getting two different instances of Person, each having a different instance of Email attached.
The original query was as follows:
SELECT p FROM Person p WHERE UPPER(p.username) = :username
Thanks again.
AG
I don't know how you wrote your personFacade and got the Person entity.
But I guess you used query.
If you are using JPQL, try fetch join.