java - REST API is unable to work in JSON format -
i new developing rest api
java. made simple 1 using mkyong
tutorials, says "hello". using apache tomcat.
now trying develop 1 return json
objects. attempting example here - http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/. of code changed (ex: package name).below code.
track.java
package com.tutorialspoint; /** * * @author yohan */ public class track { string title; string singer; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getsinger() { return singer; } public void setsinger(string singer) { this.singer = singer; } @override public string tostring() { return "track [title=" + title + ", singer=" + singer + "]"; } }
jsonservice.java
package com.tutorialspoint; import javax.ws.rs.consumes; import javax.ws.rs.get; 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; /** * * @author yohan */ @path("/json/metallica") public class jsonservice { @get @path("/get") @produces(mediatype.application_json) public track gettrackinjson() { track track = new track(); track.settitle("enter sandman"); track.setsinger("metallica"); return track; } @post @path("/post") @consumes(mediatype.application_json) public response createtrackinjson(track track) { string result = "track saved : " + track; return response.status(201).entity(result).build(); } }
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>restful web application</display-name> <servlet> <servlet-name>newservlet</servlet-name> <servlet-class>newservlet</servlet-class> </servlet> <servlet> <servlet-name>jersey restful application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.tutorialspoint</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.pojomappingfeature</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>newservlet</servlet-name> <url-pattern>/newservlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>jersey restful application</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app>
my application not marven
project. added libraries below.
i tested application below url.
http://localhost:8080/restful_api_sample/rest/json/metallica/get
all got below page
any idea why not getting expected result?
currently don't have json/pojo support. jersey-json
jar , pojomappingfeature
configuration in web.xml jersey 1.x, won't work jersey 2.x. jars need jersey 2.x support, see this answer.
Comments
Post a Comment