Service Class -
package org.sameer.learnSpringBoot.topic;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;
#Service
public class TopicService {
private List<Topic> topics = new ArrayList<>(Arrays.asList(new Topic("Spring", "Spring Framework", "Description for Spring"),
new Topic("Hibernate", "Hibernate Framework", "Description For Hibernate"),
new Topic("CoreJava", "Core Java Framework", "Description For CoreJava"),
new Topic("Servlets", "Servlets Framework", "Description for Servlets")));
public List<Topic> getAllTopic() {
return topics;
}
public Topic getTopic(String id) {
return topics.stream().filter(t -> t.getId().equals(id)).findFirst().get();
}
public void addTopic(Topic topic) {
topics.add(topic);
}
}
Controller Class -
#RequestMapping(method = RequestMethod.POST ,value="/topics")
public void addTopic(#RequestBody Topic topic) {
topicService.addTopic(topic);
}
Model Class Topic -
package org.sameer.learnSpringBoot.topic;
public class Topic {
private String id;
private String name;
private String description;
public Topic() {
}
public Topic(String id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
I am not able to post using POSTMAN plugin from chrome.
enter image description here
And Getting the same in Satacktrace -
2018-02-17 15:57:59.841 WARN 4328 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void org.sameer.learnSpringBoot.topic.TopicController.addTopic(org.sameer.learnSpringBoot.topic.Topic)
2018-02-17 15:58:08.501 WARN 4328 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void org.sameer.learnSpringBoot.topic.TopicController.addTopic(org.sameer.learnSpringBoot.topic.Topic)
I am learning Spring Boot. I am trying to HTML Put values to my topics page
CAN ANYBODY PLEASE LET ME KNOW WHAT I AM MISSING HERE??? GET WORKS FINE
STS -4.7.1
JRE - 1.8
Your request body is missing so you are getting BAD REQUEST Error. in request header u have set "Content-type" as application/json. There is a tab named body next to it. click on that and provide the json input body for Topic.
{
"id":"102",
"name":"math",
"description":"asdf"
}
Related
Creating an restful application but it is not returning the response in XML. Even there is no log on the console when hitting the URL "http://localhost:8080/message/webapi/messages".
I am returning a list and using #Produces(MediaType.APPLICATION_XML) to return the response in XML.
MessageResource.java
package org.porwal.restful.message.resources;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.porwal.restful.message.model.Message;
import org.porwal.restful.message.service.MessageService;
#Path("/messages")
public class MessageResource {
MessageService ms = new MessageService();
#GET
#Produces(MediaType.APPLICATION_XML)
public List<Message> getMessage(){
return ms.getAllMessage();
}
}
Message.java
package org.porwal.restful.message.model;
import java.util.Date;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement( name = "Message" )
public class Message {
public long id;
public String message;
public Date created;
public String author;
public Message() {
}
public Message(long id, String message, String author) {
this.id = id;
this.message = message;
this.author = author;
this.created = new Date();
}
public long getId() {
return id;
}
#XmlElement (name = "ID")
public void setId(long id) {
this.id = id;
}
public String getMessage() {
return message;
}
#XmlElement (name = "Message")
public void setMessage(String message) {
this.message = message;
}
public Date getCreated() {
return created;
}
#XmlElement (name = "Created")
public void setCreated(Date created) {
this.created = created;
}
public String getAuthor() {
return author;
}
#XmlElement (name = "Author")
public void setAuthor(String author) {
this.author = author;
}
}
This is working if i do not use #XMLRootElement annotation and TEXT_PLAIN is returned well through the URL. I also tried to remove #XmlElement for each fields but no luck. When i remove #XMLRootElement then MessageBodyWriter error can be seen in logs on eclipse console but when includes #XMLRootElement then no logs on eclipse console and URL "http://localhost:8080/message/webapi/messages" throws the error:
Error in case of #XmlRootElement is missing.
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=java.util.List<org.porwal.restful.message.model.Message>. This exception comes only when i commented the line "//#XmlRootElement( name = "Message" )".
HTTP Status 500 – Internal Server Error
Can someone please tell what i am missing here?
You need to make all your fields in the Message class private. If you leave them as public, then JAXB will treat it as a property, and will consider it duplicate properties as you also haves JavaBean properties (getters/setters).
#XmlRootElement( name = "Message" )
public class Message {
private long id;
private String message;
private Date created;
private String author;
// ...
}
How I figured this out was by using a generic ExceptionMapper
#Provider
public class DebugExceptionMapper implements ExceptionMapper<Exception> {
#Override
public Response toResponse(Exception exception) {
exception.printStackTrace();
return Response.serverError().entity(exception.getMessage()).build();
}
}
You can register this with your application and it will catch unmapped exceptions and you can do whatever you want with it. Here we just print the stack trace. If we don't handle it, it will just get swallowed up and we will never know what happened.
When running the app with the ExceptionMapper, here's the error message I got.
Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions
Class has two properties of the same name "author"
this problem is related to the following location:
at public java.lang.String com.example.Message.getAuthor()
at com.example.Message
this problem is related to the following location:
at public java.lang.String com.example.Message.author
at com.example.Message
Class has two properties of the same name "created"
this problem is related to the following location:
at public java.util.Date com.example.Message.getCreated()
at com.example.Message
this problem is related to the following location:
at public java.util.Date com.example.Message.created
at com.example.Message
Class has two properties of the same name "id"
this problem is related to the following location:
at public long com.example.Message.getId()
at com.example.Message
this problem is related to the following location:
at public long com.example.Message.id
at com.example.Message
Class has two properties of the same name "message"
this problem is related to the following location:
at public java.lang.String com.example.Message.getMessage()
at com.example.Message
this problem is related to the following location:
at public java.lang.String com.example.Message.message
at com.example.Message
You can clearly see what the problem is. And aside from avoiding this error, this is how encapsulation is supposed to work anyway; the fields should be private and exposed via getters and setters.
JSON Request:
{
"notificationType" : "ISSUER_OTP1ee2asasa",
"content" : "hi fff this is fff template content for SBI email good and mobile dfdfdfd and remaining balance is 333 and your name is hahaha.",
"medium" : "EMAIL",
"asa":"ddddd",
"":""
}
POJO:
package com.innoviti.notification.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
#Document(collection = "NotificationTemplate")
#JsonIgnoreProperties(ignoreUnknown=false)
public class NotificationTemplate {
#JsonCreator
public NotificationTemplate(#JsonProperty(value="notificationType",required=true)String notificationType,
#JsonProperty(value="content",required=true)String content, #JsonProperty(value="medium",required=true)String medium) {
super();
this.notificationType = notificationType;
this.content = content;
this.medium = medium;
}
#Override
public String toString() {
return "NotificationTemplate [id=" + id + ", templateId=" + templateId + ", notificationType="
+ notificationType + ", content=" + content + ", medium=" + medium + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Id
private String id;
private String templateId;
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
private String notificationType;
private String content;
private String medium;
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
public String getNotificationType() {
return notificationType;
}
public void setNotificationType(String notificationType) {
this.notificationType = notificationType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Controller where payload is posted.
#PostMapping(value = "/config", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<NotificationTemplate> configureTemplate(
#Valid #RequestBody NotificationTemplate notificationTemplate) {
NotificationTemplate notificationTemplatePersisted = null;
logger.info(
"Printing payload of template on server side" + ">>>" + notificationTemplate.toString());
try {
validatePayLoad(notificationTemplate);
notificationTemplatePersisted =
notificationTemplateService.createNotificationTemplate(notificationTemplate);
} catch (Exception de) {
logger.info(String.format("Error in saving template", de.getMessage()));
throw new RequestNotCompletedException(de.getLocalizedMessage());
}
return new ResponseEntity<NotificationTemplate>(notificationTemplatePersisted,
HttpStatus.CREATED);
}
Question:How do I validate that an uknown property has been sent as part of payload.In Existing implementation,#RequestBody maps the json without any issue.I want to throw error or validate payload if incoming json is not confirming exactly to POJO.For e.g in payload example i gave,I want to be able to throw error saying that asa is not recognized property
The Jackson property that controls this behaviour is FAIL_ON_UNKNOWN_PROPERTIES. This needs to be true in your case, to get the behaviour you describe.
It seems that since spring boot 1.2 this is set to false by default.
To set it to true add this line to your application.properties file:
spring.jackson.deserialization.fail-on-unknown-properties=true
And then you will get a JsonMappingException when there are extraneous properties in a JSON payload
One can add this class int their project and it would throw an exception if json is mismatched to the pojo class properties.
#Configuration
public class Config implements InitializingBean {
#Autowired
private RequestMappingHandlerAdapter converter;
#Override
public void afterPropertiesSet() throws Exception {
configureJacksonToFailOnUnknownProperties();
}
private void configureJacksonToFailOnUnknownProperties() {
MappingJackson2HttpMessageConverter httpMessageConverter = converter.getMessageConverters().stream()
.filter(mc -> mc.getClass()
.equals(MappingJackson2HttpMessageConverter.class))
.map(mc -> (MappingJackson2HttpMessageConverter) mc)
.findFirst()
.get();
httpMessageConverter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
}
I have created a rest api in eclipse as a maven project.
MobileAnalyticsModel class for rest api is
package org.subhayya.amazonws.mobileanalytics;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class MobileAnalyticsModel {
private String name;
private Date created;
private String location;
private String prize;
private String requirement;
public MobileAnalyticsModel() {
}
public MobileAnalyticsModel(String name, String location, String prize, String requirement) {
this.name = name;
this.location = location;
this.prize = prize;
this.requirement = requirement;
this.created = new Date();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
public String getRequirement() {
return requirement;
}
public void setRequirement(String requirement) {
this.requirement = requirement;
}
}
this is the json response of the created api:
and
this is my sample test code for invoking rest api:
package org.subhayya.example;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
public class SampleTestREstClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient( );
String reponse = client.target("http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/mobileanalyticsjson")
.request(MediaType.APPLICATION_JSON)
.get(String.class);
System.out.println(reponse);
}}
then i got full json response.. as
[{"created":"2017-03-30T14:36:58.56","location":"http://api.server.com","name":"Mobile Analytics","prize":"$1.00 per 1,000,000 Amazon Mobile Analytics events per month thereafter","requirement":"PutEvents"}]
But I want to have the single parameter as my output, for e.g., name, location or requirement.I am creating client invoking code also in the same maven project. So I wrote my code as below
Client client = ClientBuilder.newClient( );
MobileAnalyticsModel reponse =
client.target("http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/mobileanalyticsjson")
.request(MediaType.APPLICATION_JSON)
.get(MobileAnalyticsModel.class);
System.out.println(reponse.getName());
But I am getting exception, So I changed it to System.out.println(reponse);
) get atleast JSON response, then also getting error.
how do I get single name parameter from the JSON response? I am new to this rest api..please help me to fix this as soon as possible.thanks in advance
Your response is a string. The simplest way to access elements of your JSON-response is to convert the resonse to a Json-Object. Then you can access the fields easily by their name.
Have a look at:
How to parse JSON in Java
You can also check the below link to convert json to object.
Parse a JSON response as an object
This code works for me..
String url = "http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/";
String city = "mobileanalyticsjson";
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.register(JsonProcessingFeature.class).target(url);
JsonArray jsonArray = webTarget.path(city)
.request(MediaType.APPLICATION_JSON_TYPE).get(JsonArray.class);
for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) {
System.out.println(jsonObject.getString("name"));
System.out.println(jsonObject.getString("location")); }
I built a REST API Service using Java Spring Cloud / Boot. Firstly, I made a simple class connected to a MongoDB and a controller with service that should allow me to add, delete, update and get all the objects. When using POSTMAN these all work, however when I want to add or update an object using redux and fetch API I get a status 400 and "bad request" error. This seems to have something to do with the JSON I'm sending in the body but it is the exact same format of JSON that is working with for example POSTMAN.
My action in Redux. For simplicity / test purposes I added an object at the top in stead of using the object being sent from the page.
var assetObject = {
"vendor" : "why dis no work?",
"name": "wtf",
"version": "231",
"category" : "qsd",
"technology" : "whatever"
}
export function addAsset(access_token, asset) {
return dispatch => {
fetch(constants.SERVER_ADDRESS + '/as/asset/add',
{
method: 'POST',
credentials: 'include',
headers: {
'Authorization': 'Bearer' + access_token,
'Content-Type': 'application/json'
},
body: assetObject
})
.then(res => dispatch({
type: constants.ADD_ASSET,
asset
}))
}
}
Controller code in Java Spring:
#RequestMapping(method = RequestMethod.POST, path = "/add")
public void addAsset(#RequestBody Asset asset) {
assetService.addAsset(asset);
}
Status ok while doing it in postman:
The error I get when using Redux / Fetch API (I only removed the directory structure because it has company name in it):
Have been stuck on this for a while, any help is much appreciated!
EDIT Asset Object:
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "assets")
public class Asset {
#Id
private String id;
private String vendor;
private String name;
private String version;
private String category;
private String technology;
public Asset() {
}
public Asset(String id,
String vendor,
String name,
String version,
String category,
String technology) {
this.id = id;
this.vendor = vendor;
this.name = name;
this.version = version;
this.category = category;
this.technology = technology;
}
public String getId() {
return id;
}
public String getVendor() {
return vendor;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public String getCategory() {
return category;
}
public String getTechnology() {
return technology;
}
public void setId(String id) {
this.id = id;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
public void setName(String name) {
this.name = name;
}
public void setVersion(String version) {
this.version = version;
}
public void setCategory(String category) {
this.category = category;
}
public void setTechnology(String technology) {
this.technology = technology;
}
}
your error message says :
; required request body is missing
i think the error happens when your controller method
trying to form an object from the incoming request.
when you are sending the request you have to set each and every field related to the object.
if you are planning on not setting a property you should mark that field with #JsonIgnore annotation.
you can use #JsonIgnore annotation on the variable which will ignore this property
when forming the object as well as when outputing the object.
use #JsonIgnore annotation on the setter method , which i think you should do now since
you are ignoring the id property when making the request.
#JsonIgnore
public void setId(String id) {
this.id = id;
}
and you can return httpstatus code from the controller method,
so that client knows request was successful
#ResponseBody
public ResponseEntity<String> addAsset(#RequestBody Asset asset) {
return new ResponseEntity<String>("your response here", HttpStatus.OK);
}
I tried the Spring Guide Accessing Data with MongoDB. What I can't figure out is how do I configure my code to not use the default server address and not use the default database. I have seen many ways to do it with XML but I am trying to stay with fully XML-less configurations.
Does anyone have an example that sets the server and database without XML and can be easily integrated into the sample they show in the Spring Guide?
Note: I did find how to set the collection (search for the phrase "Which collection will my documents be saved into " on this page.
Thank you!
p.s. same story with the Spring Guide for JPA -- how do you configure the db properties -- but that is another post :)
It would be something like this for a basic configuration :
#Configuration
#EnableMongoRepositories
public class MongoConfiguration extends AbstractMongoConfiguration {
#Override
protected String getDatabaseName() {
return "dataBaseName";
}
#Override
public Mongo mongo() throws Exception {
return new MongoClient("127.0.0.1", 27017);
}
#Override
protected String getMappingBasePackage() {
return "foo.bar.domain";
}
}
Example for a document :
#Document
public class Person {
#Id
private String id;
private String name;
public Person(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Example for a repository :
#Repository
public class PersonRepository {
#Autowired
MongoTemplate mongoTemplate;
public long countAllPersons() {
return mongoTemplate.count(null, Person.class);
}
}