i found myself in a situation where i need to get some data with POST on my server and do some simple stuff with it. But it keeps saying 415 Unsupported Media Type when i try to test it with www.hurl.it website.
This is my json that i am sending:
{
"pictures": {
"picture": [
{
"id": "1",
"name": "10_aut_linen_male_less_student_work_yellow_n_cold.png"
},
{
"id": "2",
"name": "10_aut_linen_male_less_student_work_yellow_n_mild.png"
},
{
"id": "3",
"name": "10_aut_linen_male_less_student_work_yellow_n_hot.png"
}
]
}
}
this is the model class:
package com.models.sm7;
import java.util.ArrayList;
import java.util.List;
public class Sm7Pictures {
private Pictures pictures;
public Pictures getPictures() {
return pictures;
}
public void setPictures(Pictures pictures) {
this.pictures = pictures;
}
public class Pictures {
private List<Picture> picture = new ArrayList<Picture>();
public List<Picture> getPicture() {
return picture;
}
public void setPicture(List<Picture> picture) {
this.picture = picture;
}
}
public class Picture {
private String id;
private String 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;
}
}
}
This is my service class :
#Path("sm7Service")
public class Sm7Service {
Sm7DAO sm7DAO = new Sm7DAO();
#GET
#Path("/testSm7")
#Produces(MediaType.TEXT_PLAIN)
public String testSm (){
return "Hello";}
#POST
#Path("/checkPictures")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.TEXT_PLAIN)
public String getPictures(Sm7Pictures picture){
System.out.println("pokrenuto");
return "Hello";}
}
the DAO class is where i will do all my logic, but for testing purpouses i've just put text plain... I am really banging my head over this :S because it is simple but i can not get it to work. my bet is that it has something to do with the model class? Please help!
Solved it, had to put {} inside #Consumes(MediaType.APPLICATION_JSON) like this #Consumes({MediaType.APPLICATION_JSON}).
Related
Iam trying t get values from a HashMap but when i call him and setText the value i always get Null, let me show the code:
MyValues.class
private List<ItemsBean> items;
public List<ItemsBean> getItems() { return items;}
public void setItems(List<ItemsBean> items) { this.items = items; }
public static class ItemsBean {
private Map<String, leakBean> Gitt;
public Map<String, leakBean> getGitt() { return Gitt;}
public void setGitt(Map<String, leakBean> Gitt) { this.Gitt = Gitt;}
public static class leakBean {
private int id;
private String dev;
public int getId() {return id; }
public String getDev(){return dev;}
public void setId(int id) { this.id = id;}
public void setDev(String dev){this.dev = dev;}
}
I´m using Gson so for get the values and use it for .setText or Toast im trying to access like this:
MyValues object;
txt1.setText(String.valueOf( object.getItems().get(0).getGitt().get("id")));
Here i get null, can someone helpme with this? i just need to access to values, also the items.size(); return 1 and must return 3
here is hte JSON:
{
"id": 1001,
"name": "Super1",
"user": {
"name": "The Super 1"
},
"items": [
{
"987987M7812b163eryrt": {
"id": 1,
"dev": "seed"
},
"90812bn120893juuh": {
"id": 2,
"dev": "none"
},
"981273jn19203nj123rg": {
"id": 3,
"dev": "mine"
}
}
]}
Try with these POJOs:
MyValues.java
public class MyValues {
private int id;
private String name;
private User user;
private List<HashMap<String, ItemsBean>> items;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<HashMap<String, ItemsBean>> getItems() {
return items;
}
public void setItems(List<HashMap<String, ItemsBean>> items) {
this.items = items;
}
}
User.java
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ItemsBean.java
public class ItemsBean {
private int id;
private String dev;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDev() {
return dev;
}
public void setDev(String dev) {
this.dev = dev;
}
}
You can no access the values like so:
object.getItems().get(0).get("987987M7812b163eryrt").getId();
I have followed this tutorial to build REST API using Spring boot. It taught alot. But What I am trying to do really got me stuck.
What I am trying to get is:
{
"marks":{
"id":"1",
"name":"test",
"remark":"passed",
"course": {
"id": "1",
"name": "Spring Boot",
"description": "Solves many problems",
"topic": {
"id": "1",
"name": "Java",
"description": "Powerful Programming Language"
}
}
But I get the error when I tried to add the marks- as :
{
"timestamp": 1515600105327,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.bind.MissingPathVariableException",
"message": "Missing URI template variable 'courseId' for method parameter of type String",
"path": "/topics/1/courses/1/marks"
}
My Marks Model is:
public class Marks {
#Id
private String id;
private String name;
private String remark;
#ManyToOne
private Course course;
#ManyToOne
private Topic topic;
public Marks() {
}
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
public Marks(String id, String name, String remark,String topicId, String courseId) {
this.id = id;
this.name = name;
this.remark = remark;
this.topic = new Topic(topicId, "","");
this.course = new Course(courseId, " ", " ", " ");
}
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 getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
And MarksController.java:
public class MarksController {
#RestController
public class MarksController {
#Autowired
private MarksService marksService;
#RequestMapping("/topics/{topicId}/courses/{id}/marks")
public List<Marks> getAllMarks(#PathVariable String courseId) {
return marksService.getAllMarks(courseId);
}
#RequestMapping(method=RequestMethod.POST, value="/topics/{topicId}/courses{courseId}/marks")
public void addMarks(#RequestBody Marks marks,#PathVariable String topicId ,#PathVariable String courseId) {
marks.setTopic(new Topic(topicId, "", ""));
marks.setCourse(new Course(courseId, "", "", ""));
marksService.addMarks(marks);
}
}
And MarksService.java:
public class MarksService {
#Service
public class MarksService {
#Autowired
private MarksRepository marksRepository;
public void addMarks(Marks marks) {
marksRepository.save(marks);
}
}
And MarksRepository.java:
public interface MarksRepository extends CrudRepository<Marks, String> {
public List<Marks> findByCourseId(String courseId);
public List<Marks> findByTopicId(String topicId);
}
Can anyone help me get the result as in the mentioned JSON.
For the POST method
This:
/topics/{topicId}/courses{courseId}/marks
Should be:
/topics/{topicId}/courses/{courseId}/marks
Note the additional / between courses and {courseId}
For the GET method
This:
/topics/{topicId}/courses/{id}/marks
Should be:
/topics/{topicId}/courses/{courseId}/marks
Note the use of courseId to agree with the parameter name in MarksController.getAllMarks.
So I have a input Json data-
[
{
"Id": "30B05CEC6808F81C7A8B",
"eventType" :"TestEventType"
}
]
And a wrapper class-
public class CancelSeatEventDtoWrapper {
List<CancelSeatEventDto> cancelSeatDtoList;
public List<CancelSeatEventDto> getCancelSeatDtoList() {
return cancelSeatDtoList;
}
public void setCancelSeatDtoList(List<CancelSeatEventDto> cancelSeatDtoList) {
this.cancelSeatDtoList = cancelSeatDtoList;
}
}
And a POJO
public class CancelSeatEventDto {
private String eventType, Id;
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}
}
And in a Spring controller one of the method has -
#RequestMapping(value = "/teams/id.do", method = RequestMethod.DELETE)
#ResponseBody
public String deleteId(#RequestBody CancelSeatEventDtoWrapper jsonValue)
After running it i get a 400 Bad Request error.
Any idea whats wrong?
Model
public class Organisation {
private String name;
public Organisation() { }
public Organisation(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
controller
#RequestMapping(method = RequestMethod.GET)
public List<Organisation> getAll() {
Organisation organisation = new Organisation("google");
List<Organisation> organisations = new ArrayList<>();
organisations.add(organisation);
return organisations;
}
This will give out response like this:
[
{
"name": "google"
}
]
What if we want something like this:
{
"data": [{
"type": "organisations"
"attributes": {
"name": "google"
}
]
}
So how to customize the json. I know that Spring MVC by default uses Jackson to convert models into JSON. Is there a way to customize it. I am trying to send response in JSONApi standard. Also can someone tell how to create links in responses
Create Classes as:
public class Object1 {
private List<Object2> data;
public Object1() {
}
public Object1(List<Object2> data) {
this.data = data;
}
//getters and setters
}
public class Object2 {
private String type;
private Object3 attributes;
public Object2() {
}
public Object2(String type, Object3 attributes) {
this.type = type;
this.attributes = attributes;
}
//getters and setters
}
public class Object3 {
private String name;
public Object3(String name) {
this.name = name;
}
public Object3() {
}
//getters and setters
}
Now your controller method shoul be like:
#RequestMapping(method = RequestMethod.GET)
public Object3 getAll() {
List<Object2> data = new ArrayList<>();
data.add(new Object2("organisations", new Object3("google")));
return new Object1(data);
}
My problem:
I`ve built a rest client within my iphone-application with restkit 0.20.
My RESTService was built with jax-rs including jackson.
There is one class named "Customer", where I hold my customer data.
#XmlRootElement
public class Customer extends BaseEntity {
private int customer_id;
private String appell;
private String firstname;
private String lastname;
private Date birthday;
private String street;
private String streetnr;
private String zipcode;
private String place;
public int getCustomer_id() {
return customer_id;
}
public void setCustomer_id(int customer_id) {
this.customer_id = customer_id;
}
public String getAppell() {
return appell;
}
public void setAppell(String appell) {
this.appell = appell;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getStreetnr() {
return streetnr;
}
public void setStreetnr(String streetnr) {
this.streetnr = streetnr;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
What can I do? Why there's always this error logged in RestKit:
Expected status code in (200-299), got 400" UserInfo=0x956e2f0 {NSLocalizedRecoverySuggestion=Unrecognized field "customer" (Class customer.model.Customer), not marked as ignorable
I don't have a field named customer.
Here is my POST method:
#POST
#Path("/insert")
#Consumes(MediaType.APPLICATION_JSON)
public Response insertCustomer(Customer customer) {
String result = "Customer inserted " + customer.getLastname();
customer.createEntity();
return Response.status(201).type(MediaType.APPLICATION_JSON).entity(result).build();
}
This is an example of a generated JSON object (Jackson), when a #GET method is called within my ios application.
{
"appell": "Frau",
"birthday": "1991-10-01T00:00:00+01:00",
"customer_id": "3",
"firstname": "Max",
"lastname": "Mustermann",
"place": "München",
"street": "Einsteinweg",
"streetnr": "19",
"zipcode": "81858"
}
And this is an example of the JSON object, when my ios application calls the showed #POST method:
{"customer":{"streetnr":"19",
"zipcode":"81829",
"customer_id":0,
"place":"München",
"appell":"Herr",
"firstname":"Heinrich",
"birthday":"1991-10-01T00:00:00+01:00",
"lastname":"Maier",
"street":"Lehrer-Wirth-Str.19"}}
I am a absolute newbie using restkit and jax-rs.
I just added a #GET method for requesting xml format.
Here you can see it:
#GET
#Path("/listallcustomers")
#Produces(MediaType.APPLICATION_XML)
public ArrayList<Customer> showAllCustomers() {
ArrayList<Customer> list = DBAccess.getInstance().getAllCustomers();
return list;
}
and this is the result:
<customers>
<customer>
<appell>Frau</appell>
<birthday>1991-10-01T00:00:00+01:00</birthday>
<customer_id>3</customer_id>
<firstname>Max</firstname>
<lastname>MUstermann</lastname>
<place>München</place>
<street>Einsteinstr.</street>
<streetnr>19</streetnr>
<zipcode>82838</zipcode>
</customer>
...
My #GET method which creates a JSON object is the following:
#GET
#Path("/listall")
#Produces(MediaType.APPLICATION_JSON)
public ArrayList<Customer> showAllJsonCustomers() {
ArrayList<Customer> list = DBAccess.getInstance().getAllCustomers();
return list;
}
and this an example of a JSON object it creates:
{
"lastname": "Müller",
"customer_id": 6,
"appell": "Herr",
"firstname": "pupsi",
"birthday": "1986-10-16",
"street": "Lehrer-Wirth-Str",
"streetnr": "19",
"zipcode": "81829",
"place": "München"
},
...