HTTP REST Jersey - PUT-Beispiel von Client senden

hiwi

Mitglied
Hallo liebes Forum,

ich versuche mich gerade in Jersey einzuarbeiten. Dabei habe ich folgendes Tutorial gemacht:
Tutorial - REST API design and implementation with Jersey and Spring | Codingpedia.org

So ganz steige ich noch nicht durch, durch die ganzen Schichten und Pattern, die angewendet werden. Die Lösung des konkreten Problems wird mir vielleicht dabei weiterhelfen.

Ich habe unter anderem:
Eine Klasse Podcast
Java:
@SuppressWarnings("restriction")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Podcast implements Serializable {

	private static final long serialVersionUID = -8039686696076337053L;

	/** id of the podcast */
	@XmlElement(name = "id")	
	private Long id;
	
	/** title of the podcast */
	@XmlElement(name = "title")	
	private String title;
		
	/** link of the podcast on Podcastpedia.org */
	@XmlElement(name = "linkOnPodcastpedia")	
	private String linkOnPodcastpedia;
	
	/** url of the feed */
	@XmlElement(name = "feed")	
	private String feed;
	
	/** description of the podcast */
	@XmlElement(name = "description")
	@PodcastDetailedView	
	private String description; 
		
	/** insertion date in the database */
	@XmlElement(name = "insertionDate")
	@XmlJavaTypeAdapter(DateISO8601Adapter.class)	
	@PodcastDetailedView
	private Date insertionDate;

	public Podcast(PodcastEntity podcastEntity){
		try {
			BeanUtils.copyProperties(this, podcastEntity);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Podcast(String title, String linkOnPodcastpedia, String feed,
			String description) {
		
		this.title = title;
		this.linkOnPodcastpedia = linkOnPodcastpedia;
		this.feed = feed;
		this.description = description;
		
	}
	
	public Podcast(){}
//Getters und Setters

Eine Klasse PodcastEntity
Java:
@Entity
@Table(name="podcasts")
public class PodcastEntity implements Serializable {

	private static final long serialVersionUID = -8039686696076337053L;

	/** id of the podcast */
	@Id
	@GeneratedValue
	@Column(name="id")
	private Long id;
	
	/** title of the podcast */
	@Column(name="title")
	private String title;
		
	/** link of the podcast on Podcastpedia.org */
	@Column(name="link_on_podcastpedia")
	private String linkOnPodcastpedia;
	
	/** url of the feed */
	@Column(name="feed")
	private String feed;
	
	/** description of the podcast */
	@Column(name="description")
	private String description; 
		
	/** insertion date in the database */
	@Column(name="insertion_date")
	private Date insertionDate;

	public PodcastEntity(){}
	
	public PodcastEntity(String title, String linkOnPodcastpedia, String feed,
			String description) {
		
		this.title = title;
		this.linkOnPodcastpedia = linkOnPodcastpedia;
		this.feed = feed;
		this.description = description;
		
	}
	
	public PodcastEntity(Podcast podcast){
		try {
			BeanUtils.copyProperties(this, podcast);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
//Getters und Setters
Und eine Klasse PodcastResource, bei der ich hier nur die POST- und PUT- beantwortenden Methoden angegeben habe.
Java:
@Component
@Path("/podcasts")
public class PodcastsResource {

	@Autowired
	private PodcastService podcastService;

	/*
	 * *********************************** CREATE ***********************************
	 */

	/**
	 * Adds a new resource (podcast) from the given json format (at least title
	 * and feed elements are required at the DB level)
	 * 
	 * @param podcast
	 * @return
	 * @throws AppException
	 */
	@POST
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.TEXT_HTML })
	public Response createPodcast(Podcast podcast) throws AppException {
		Long createPodcastId = podcastService.createPodcast(podcast);
		return Response.status(Response.Status.CREATED)// 201
				.entity("A new podcast has been created")
				.header("Location",
						"http://localhost:8888/demo-rest-jersey-spring/podcasts/"
								+ String.valueOf(createPodcastId)).build();
	}

	/**
	 * Adds a new podcast (resource) from "form" (at least title and feed
	 * elements are required at the DB level)
	 * 
	 * @param title
	 * @param linkOnPodcastpedia
	 * @param feed
	 * @param description
	 * @return
	 * @throws AppException
	 */
	@POST
	@Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
	@Produces({ MediaType.TEXT_HTML })
	@Transactional
	public Response createPodcastFromApplicationFormURLencoded(
			@FormParam("title") String title,
			@FormParam("linkOnPodcastpedia") String linkOnPodcastpedia,
			@FormParam("feed") String feed,
			@FormParam("description") String description) throws AppException {

		Podcast podcast = new Podcast(title, linkOnPodcastpedia, feed,
				description);
		Long createPodcastid = podcastService.createPodcast(podcast);

		return Response
				.status(Response.Status.CREATED)// 201
				.entity("A new podcast/resource has been created at /demo-rest-jersey-spring/podcasts/"
						+ createPodcastid)
				.header("Location",
						"http://localhost:8888/demo-rest-jersey-spring/podcasts/"
								+ String.valueOf(createPodcastid)).build();
	}

	/**
	 * A list of resources (here podcasts) provided in json format will be added
	 * to the database.
	 * 
	 * @param podcasts
	 * @return
	 * @throws AppException
	 */
	@POST
	@Path("list")
	@Consumes({ MediaType.APPLICATION_JSON })
	public Response createPodcasts(List<Podcast> podcasts) throws AppException {
		podcastService.createPodcasts(podcasts);
		return Response.status(Response.Status.CREATED) // 201
				.entity("List of podcasts was successfully created").build();
	}


	/*
	 * *********************************** UPDATE ***********************************
	 */

	/**
	 * The method offers both Creation and Update resource functionality. If
	 * there is no resource yet at the specified location, then a podcast
	 * creation is executed and if there is then the resource will be full
	 * updated.
	 * 
	 * @param id
	 * @param podcast
	 * @return
	 * @throws AppException
	 */
	@PUT
	@Path("{id}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.TEXT_HTML })
	public Response putPodcastById(@PathParam("id") Long id, Podcast podcast)
			throws AppException {

		Podcast podcastById = podcastService.verifyPodcastExistenceById(id);

		if (podcastById == null) {
			// resource not existent yet, and should be created under the
			// specified URI
			Long createPodcastId = podcastService.createPodcast(podcast);
			return Response
					.status(Response.Status.CREATED)
					// 201
					.entity("A new podcast has been created AT THE LOCATION you specified")
					.header("Location",
							"http://localhost:8888/demo-rest-jersey-spring/podcasts/"
									+ String.valueOf(createPodcastId)).build();
		} else {
			// resource is existent and a full update should occur
			podcastService.updateFullyPodcast(podcast);
			return Response
					.status(Response.Status.OK)
					// 200
					.entity("The podcast you specified has been fully updated created AT THE LOCATION you specified")
					.header("Location",
							"http://localhost:8888/demo-rest-jersey-spring/podcasts/"
									+ String.valueOf(id)).build();
		}
	}

}

In der Testklasse werden nur GET-Anfragen vom Client gesendet. Und ich weiß nicht, wie ich die POST- und PUT-Anfragen senden soll, sodass die entsprechenden Methoden aus der letzten Klasse angesprochen werden und wann ich welche am besten verwende.
Meine Versuche haben mir bisher folgende Antwort gegeben:
.2014-12-31 15:13:39,929 [qtp193852045-58] DEBUG o.c.d.r.f.LoggingResponseFilter - Requesting POST for path podcasts/
.2014-12-31 15:13:40,177 [qtp193852045-58] DEBUG o.c.d.r.f.LoggingResponseFilter - Response {
"status" : 406,
"code" : 5001,
"message" : "HTTP 406 Not Acceptable",
"link" : "http://www.codingpedia.org/ama/tutorial-rest-api-design-and-implementation-in-java-with-jersey-and-spring/",
"developerMessage" : "javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable\n\tat org.glassfish.jersey.server.internal.routing.MethodSelectingRouter.getMethodRouter(MethodSelectingRouter.java:573)\n\tat org.glassfish.jersey.server.internal.routing.MethodSelectingRouter.access$300(MethodSelectingRouter.java:94)\n\tat org.glassfish.jersey.server.internal.routing.MethodSelectingRouter$4.apply(MethodSelectingRouter.java:814)\n\tat org.glassfish.jersey.server.internal.routing.MethodSelectingRouter.apply(MethodSelectingRouter.java:419)\n\tat org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)\n\tat org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)\n\tat org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)\n\tat org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:94)\n\tat org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:63)\n\tat org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)\n\tat org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:261)\n\tat org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)\n\tat org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)\n\tat org.glassfish.jersey.internal.Errors.process(Errors.java:315)\n\tat org.glassfish.jersey.internal.Errors.process(Errors.java:297)\n\tat org.glassfish.jersey.internal.Errors.process(Errors.java:267)\n\tat org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)\n\tat org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:252)\n\tat org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1025)\n\tat org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)\n\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)\n\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)\n\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)\n\tat org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:696)\n\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:521)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:138)\n\tat org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:564)\n\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:213)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1097)\n\tat org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:448)\n\tat org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1031)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)\n\tat org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:200)\n\tat org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)\n\tat org.eclipse.jetty.server.Server.handle(Server.java:446)\n\tat org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:271)\n\tat org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:246)\n\tat org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)\n\tat java.lang.Thread.run(Thread.java:745)\n"
}
postresponse.getStatus(): 406

Wenn mir jemand Beispiele zu einem Beispiel-Podcast
Podcast newPodcast = new Podcast("titleIsHello", "linkOnPodcastPediaIsTHIS IS A LINK", "feedISaBitFood", "descriptionHEREyouARE");
geben könnte, wäre ich sehr glücklich!!! PUT- und POST-Beispiele meine ich natürlich. Die GET-Tests funktionieren!
Hier einer meiner bisherigen Versuche:
Java:
	@Test
	public void testPutPodcast(){
		System.out.println("**************** Testing putting podcast ***************");
	
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.register(JacksonFeature.class);

		Client client = ClientBuilder.newClient(clientConfig);

		WebTarget webTarget = client
				.target("http://localhost:8888/demo-rest-jersey-spring/podcasts/");
		//("", MediaType.TEXT_PLAIN)
		//Podcast newPodcast = new Podcast("titleIsHello", "linkOnPodcastPediaIsTHIS IS A LINK", "feedISaBitFood", "descriptionHEREyouARE");
		String newPodcastJSONstringParams = "{title:titleIsHello, linkOnPodcastpedia:ThisIsTheLink, feed:ThisIsABitFood, description:ThisIsTheDescription}";
		Response postresponse = webTarget.request(MediaType.TEXT_PLAIN_TYPE).post(Entity.entity(newPodcastJSONstringParams, MediaType.APPLICATION_JSON));
	
		System.out.println("postresponse.getStatus(): " + postresponse.getStatus());
	}

Vielen vielen Dank im Voraus!
Und einen guten Rutsch allen!
 
Zuletzt bearbeitet:

Ähnliche Java Themen

Neue Themen


Oben