意外的 's' 响应宁静的服务 [英] unexpected 's' in response of restful service

查看:38
本文介绍了意外的 's' 响应宁静的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习宁静的服务,作为其中的一部分,我正在设计一个示例请求和响应页面...看我做的一切都是正确的,除了以下内容资源调用:

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();
    }

}

我使用的请求是 POST 并且 url 是 http://localhost:8080/com/rest/RestService/login/sudheer/123

and the request i am using is POST and the url is http://localhost:8080/com/rest/RestService/login/sudheer/123

我得到的回应是

<!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>

响应头

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

推荐答案

在你的 pom.xml 中添加以下依赖

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();
}

要使用@RequestBody,最好使用<mvc:annotation-driven/>

To use @RequestBody you better use <mvc:annotation-driven />

这个答案是这个的副本.所以如果它有效,请在那里投票.:)

This answer is somewhat copy of this. So please upvote there if it works. :)

这篇关于意外的 's' 响应宁静的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆