问题
i am trying to learn restful services and as part of that i am designing a sample request and response page...see i am getting everything right except the following resource called:
<i>
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/RestService")
public class CrunchifyRESTService {
@POST
@Path("/crunchifyService")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response crunchifyREST(InputStream incomingData) {
StringBuilder crunchifyBuilder = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
String line = null;
while ((line = in.readLine()) != null) {
crunchifyBuilder.append(line);
}
} catch (Exception e) {
System.out.println("Error Parsing: - ");
}
System.out.println("Data Received: " + crunchifyBuilder.toString());
// return HTTP response 200 in case of success
return Response.status(200).entity(crunchifyBuilder.toString()).build();
}
@GET
@Path("/verify")
@Produces(MediaType.APPLICATION_JSON)
public Response verifyRESTService(InputStream incomingData) {
String result = "CrunchifyRESTService Successfully started..";
// return HTTP response 200 in case of success
return Response.status(200).entity(result).build();
}
@POST
@Path("/login")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response loginserv(newUser user){
String data="";
data=user.uname+" and auth "+user.pwd;
return Response.status(200).entity(data).build();
}
}
and the request i am using is POST and the url is http://localhost:8080/com/rest/RestService/login/sudheer/123
and the response i am getting is
<!DOCTYPE html>
<html>
<head>
<title>Apache Tomcat/8.5.4 - Error report</title>
<style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style>
</head>
<body>
<h1>HTTP Status 415 - Unsupported Media Type</h1>
<div class="line"></div>
<p>
<b>type</b> Status report
</p>
<p>
<b>message</b>
<u>Unsupported Media Type</u>
</p>
<p>
<b>description</b>
<u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.</u>
</p>
<hr class="line">
<h3>Apache Tomcat/8.5.4</h3>
</body>
</html>
Response headers
Content-Language →en
Content-Length →1133
Content-Type →text/html;charset=utf-8
Date →Mon, 24 Oct 2016 06:35:48 GM
T
and know what is the exact problem with the code pls clarify me
回答1:
Unsupported media type error usually means that the Content-type
header of the request is not as server expects. In your case, the line
@Consumes(MediaType.APPLICATION_JSON)
signals that your REST API expects JSON request. You haven't posted the exact request made with Postman, but if you add in Headers section new entry with key Content-Type
and value application/json
the error should disappear. Of course, make sure that your request really is in correct JSON format - use e.g. http://jsonlint.com to validate the request. Body of the request should be something along the lines:
{
uname : "sudheer",
pwd : "123"
}
回答2:
Add the following dependencies in your pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version> // 2.4.3
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version> // 2.4.3
</dependency>
Then define your controller as :-
@POST
@Path("/login")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response loginserv(@RequestBody newUser user){
//I really hope you have newUser as your classname better follow Java conventions and rename it as NewUser
String data="";
data=user.uname+" and auth "+user.pwd;
return Response.status(200).entity(data).build();
}
To use @RequestBody you better use <mvc:annotation-driven />
This answer is somewhat copy of this. So please upvote there if it works. :)
回答3:
For POST request, you do not send data by specifying it in the path. Instead, create a wrapper class like this:
class User{
private String uname;
private String pwd;
// getter and setter methods
}
and then write the service as:
@Path("/login")
public class LoginService {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response loginserv(User user){
String data="";
data=user.uname+" and auth "+user.pwd;
return Response.status(200).entity(data).build();
}
}
I guess, the unexpected 's' is displayed because the service does not expect the path you are passing as URL.
回答4:
In your case, I notice 2 errors: 1) Your error description clearly says that, "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method"
The content-type you are consuming in your request is @Consumes(MediaType.APPLICATION_JSON), So in request you have to provide your POST data in JSON format like {"key1":"value1","key2":"value2"}.
2) You received response "unexpected 's'" because you are producing response content-type @Produces(MediaType.APPLICATION_JSON) i.e. in JSON format & at the time of sending response you are creating String by using (.toString() method)
So use @Produces (MediaType.ALL_VALUE) to get JSON as well as string in response.
回答5:
Open "Raw" tab (mode) in Postman to see exact text response. The message itself just tells "Postman can't convert received text into [whatever format you've selected] because of unexpected character".
来源:https://stackoverflow.com/questions/40211597/unexpected-s-in-response-of-restful-service